From 339493dbd88f45eee9d044123d13717558047fca Mon Sep 17 00:00:00 2001 From: Elmar Ludwig Date: Fri, 10 Jan 2025 14:55:22 +0000 Subject: add Vips as CorePlugin, re #4258 Merge request studip/studip!3432 --- app/controllers/vips/admin.php | 208 ++ app/controllers/vips/api.php | 256 ++ app/controllers/vips/config.php | 95 + app/controllers/vips/exam_mode.php | 29 + app/controllers/vips/pool.php | 473 ++++ app/controllers/vips/sheets.php | 2305 ++++++++++++++++++ app/controllers/vips/solutions.php | 2521 ++++++++++++++++++++ app/views/vips/admin/edit_block.php | 47 + app/views/vips/admin/edit_grades.php | 56 + app/views/vips/config/index.php | 90 + app/views/vips/config/pending_assignments.php | 76 + app/views/vips/exam_mode/index.php | 46 + app/views/vips/exercises/ClozeTask/correct.php | 42 + app/views/vips/exercises/ClozeTask/edit.php | 67 + app/views/vips/exercises/ClozeTask/print.php | 62 + app/views/vips/exercises/ClozeTask/solve.php | 46 + app/views/vips/exercises/ClozeTask/xml.php | 63 + app/views/vips/exercises/MatchingTask/correct.php | 88 + app/views/vips/exercises/MatchingTask/edit.php | 117 + app/views/vips/exercises/MatchingTask/print.php | 95 + app/views/vips/exercises/MatchingTask/solve.php | 38 + app/views/vips/exercises/MatchingTask/xml.php | 50 + .../vips/exercises/MatrixChoiceTask/correct.php | 42 + app/views/vips/exercises/MatrixChoiceTask/edit.php | 91 + .../vips/exercises/MatrixChoiceTask/print.php | 41 + .../vips/exercises/MatrixChoiceTask/solve.php | 27 + app/views/vips/exercises/MatrixChoiceTask/xml.php | 51 + .../vips/exercises/MultipleChoiceTask/correct.php | 32 + .../vips/exercises/MultipleChoiceTask/edit.php | 46 + .../vips/exercises/MultipleChoiceTask/print.php | 30 + .../vips/exercises/MultipleChoiceTask/solve.php | 13 + .../vips/exercises/MultipleChoiceTask/xml.php | 43 + app/views/vips/exercises/SequenceTask/correct.php | 85 + app/views/vips/exercises/SequenceTask/edit.php | 50 + app/views/vips/exercises/SequenceTask/print.php | 92 + app/views/vips/exercises/SequenceTask/solve.php | 14 + app/views/vips/exercises/SequenceTask/xml.php | 48 + .../vips/exercises/SingleChoiceTask/correct.php | 41 + app/views/vips/exercises/SingleChoiceTask/edit.php | 86 + .../vips/exercises/SingleChoiceTask/print.php | 41 + .../vips/exercises/SingleChoiceTask/solve.php | 23 + app/views/vips/exercises/SingleChoiceTask/xml.php | 51 + app/views/vips/exercises/TextLineTask/correct.php | 37 + app/views/vips/exercises/TextLineTask/edit.php | 80 + app/views/vips/exercises/TextLineTask/print.php | 35 + app/views/vips/exercises/TextLineTask/solve.php | 9 + app/views/vips/exercises/TextLineTask/xml.php | 53 + app/views/vips/exercises/TextTask/correct.php | 184 ++ app/views/vips/exercises/TextTask/edit.php | 47 + app/views/vips/exercises/TextTask/print.php | 83 + app/views/vips/exercises/TextTask/solve.php | 131 + app/views/vips/exercises/TextTask/xml.php | 61 + app/views/vips/exercises/correct_exercise.php | 39 + app/views/vips/exercises/courseware_block.php | 79 + app/views/vips/exercises/evaluation_mode_info.php | 22 + app/views/vips/exercises/flexible_input.php | 25 + app/views/vips/exercises/flexible_textarea.php | 17 + app/views/vips/exercises/print_exercise.php | 64 + app/views/vips/exercises/show_exercise_files.php | 20 + app/views/vips/exercises/show_exercise_hint.php | 12 + app/views/vips/pool/assignments.php | 25 + app/views/vips/pool/copy_exercises_dialog.php | 50 + app/views/vips/pool/exercises.php | 15 + app/views/vips/pool/list_assignments.php | 174 ++ app/views/vips/pool/list_exercises.php | 151 ++ app/views/vips/pool/move_exercises_dialog.php | 50 + app/views/vips/sheets/add_exercise_dialog.php | 26 + app/views/vips/sheets/assign_block_dialog.php | 32 + app/views/vips/sheets/assignment_type_tooltip.php | 18 + app/views/vips/sheets/content_bar_icons.php | 19 + app/views/vips/sheets/copy_assignment_dialog.php | 104 + app/views/vips/sheets/copy_assignments_dialog.php | 34 + app/views/vips/sheets/copy_exercise_dialog.php | 132 + app/views/vips/sheets/copy_exercises_dialog.php | 52 + app/views/vips/sheets/edit_assignment.php | 329 +++ app/views/vips/sheets/edit_exercise.php | 161 ++ app/views/vips/sheets/export_assignment.php | 82 + app/views/vips/sheets/import_assignment_dialog.php | 21 + app/views/vips/sheets/ip_range_tooltip.php | 26 + app/views/vips/sheets/list_assignments.php | 27 + app/views/vips/sheets/list_assignments_list.php | 207 ++ app/views/vips/sheets/list_assignments_stud.php | 117 + app/views/vips/sheets/list_exercises.php | 67 + app/views/vips/sheets/move_assignments_dialog.php | 34 + app/views/vips/sheets/move_exercises_dialog.php | 52 + app/views/vips/sheets/print_assignment.php | 106 + app/views/vips/sheets/print_assignments.php | 43 + app/views/vips/sheets/print_layout.php | 9 + app/views/vips/sheets/show_assignment.php | 179 ++ app/views/vips/sheets/show_exercise.php | 109 + app/views/vips/sheets/show_exercise_link.php | 23 + app/views/vips/sheets/start_assignment_dialog.php | 40 + app/views/vips/solutions/assignment_solutions.php | 308 +++ app/views/vips/solutions/assignments.php | 18 + app/views/vips/solutions/assignments_list.php | 190 ++ .../vips/solutions/assignments_list_student.php | 135 ++ app/views/vips/solutions/autocorrect_dialog.php | 29 + .../vips/solutions/edit_assignment_attempt.php | 34 + app/views/vips/solutions/edit_group_dialog.php | 30 + app/views/vips/solutions/edit_solution.php | 216 ++ app/views/vips/solutions/feedback_files.php | 20 + app/views/vips/solutions/feedback_files_table.php | 51 + app/views/vips/solutions/gradebook_dialog.php | 39 + app/views/vips/solutions/participants_overview.php | 225 ++ app/views/vips/solutions/show_assignment_log.php | 56 + .../vips/solutions/solution_color_tooltip.php | 4 + app/views/vips/solutions/statistics.php | 95 + .../solutions/student_assignment_solutions.php | 125 + app/views/vips/solutions/student_grade.php | 109 + .../vips/solutions/update_released_dialog.php | 42 + app/views/vips/solutions/view_solution.php | 77 + composer.json | 2 + db/migrations/6.0.40_add_vips_module.php | 485 ++++ lib/classes/SimpleORMap.php | 4 +- lib/classes/sidebar/VipsSearchWidget.php | 42 + lib/filesystem/ExerciseFolder.php | 111 + lib/filesystem/FeedbackFolder.php | 96 + lib/filesystem/ResponseFolder.php | 107 + lib/models/Courseware/BlockTypes/TestBlock.php | 125 + lib/models/FileRef.php | 17 +- lib/models/Folder.php | 27 +- lib/models/vips/ClozeTask.php | 505 ++++ lib/models/vips/DummyExercise.php | 83 + lib/models/vips/Exercise.php | 855 +++++++ lib/models/vips/MatchingTask.php | 341 +++ lib/models/vips/MatrixChoiceTask.php | 268 +++ lib/models/vips/MultipleChoiceTask.php | 196 ++ lib/models/vips/SequenceTask.php | 255 ++ lib/models/vips/SingleChoiceTask.php | 279 +++ lib/models/vips/TextLineTask.php | 271 +++ lib/models/vips/TextTask.php | 279 +++ lib/models/vips/VipsAssignment.php | 1308 ++++++++++ lib/models/vips/VipsAssignmentAttempt.php | 99 + lib/models/vips/VipsBlock.php | 92 + lib/models/vips/VipsExerciseRef.php | 137 ++ lib/models/vips/VipsGroup.php | 79 + lib/models/vips/VipsGroupMember.php | 50 + lib/models/vips/VipsSolution.php | 160 ++ lib/models/vips/VipsTest.php | 121 + lib/modules/VipsModule.php | 471 ++++ public/assets/images/choice_checked.svg | 1 + public/assets/images/choice_unchecked.svg | 1 + public/assets/images/collapse.svg | 1 + public/assets/images/expand.svg | 1 + public/assets/images/icons/black/vips.svg | 1 + public/assets/images/icons/blue/assessment-mc.svg | 1 + public/assets/images/icons/blue/edit-line.svg | 1 + public/assets/images/icons/blue/vips.svg | 1 + public/assets/images/icons/red/vips.svg | 1 + public/assets/images/icons/white/vips.svg | 1 + .../plus/screenshots/Vips/Vips_preview_1.png | Bin 0 -> 42033 bytes .../plus/screenshots/Vips/Vips_preview_2.png | Bin 0 -> 45908 bytes .../plus/screenshots/Vips/Vips_preview_3.png | Bin 0 -> 56688 bytes resources/assets/javascripts/bootstrap/vips.js | 336 +++ resources/assets/javascripts/entry-base.js | 1 + resources/assets/javascripts/init.js | 2 + resources/assets/javascripts/lib/vips.js | 122 + resources/assets/stylesheets/scss/buttons.scss | 2 +- .../stylesheets/scss/courseware/variables.scss | 1 + resources/assets/stylesheets/scss/forms.scss | 13 +- .../assets/stylesheets/scss/jquery-ui/studip.scss | 42 + resources/assets/stylesheets/scss/sidebar.scss | 2 +- resources/assets/stylesheets/scss/tables.scss | 15 +- resources/assets/stylesheets/scss/vips.scss | 592 +++++ resources/assets/stylesheets/studip.scss | 1 + .../courseware/blocks/CoursewareTestBlock.vue | 266 +++ .../courseware/containers/container-components.js | 2 + 167 files changed, 21392 insertions(+), 12 deletions(-) create mode 100644 app/controllers/vips/admin.php create mode 100644 app/controllers/vips/api.php create mode 100644 app/controllers/vips/config.php create mode 100644 app/controllers/vips/exam_mode.php create mode 100644 app/controllers/vips/pool.php create mode 100644 app/controllers/vips/sheets.php create mode 100644 app/controllers/vips/solutions.php create mode 100644 app/views/vips/admin/edit_block.php create mode 100644 app/views/vips/admin/edit_grades.php create mode 100644 app/views/vips/config/index.php create mode 100644 app/views/vips/config/pending_assignments.php create mode 100644 app/views/vips/exam_mode/index.php create mode 100644 app/views/vips/exercises/ClozeTask/correct.php create mode 100644 app/views/vips/exercises/ClozeTask/edit.php create mode 100644 app/views/vips/exercises/ClozeTask/print.php create mode 100644 app/views/vips/exercises/ClozeTask/solve.php create mode 100644 app/views/vips/exercises/ClozeTask/xml.php create mode 100644 app/views/vips/exercises/MatchingTask/correct.php create mode 100644 app/views/vips/exercises/MatchingTask/edit.php create mode 100644 app/views/vips/exercises/MatchingTask/print.php create mode 100644 app/views/vips/exercises/MatchingTask/solve.php create mode 100644 app/views/vips/exercises/MatchingTask/xml.php create mode 100644 app/views/vips/exercises/MatrixChoiceTask/correct.php create mode 100644 app/views/vips/exercises/MatrixChoiceTask/edit.php create mode 100644 app/views/vips/exercises/MatrixChoiceTask/print.php create mode 100644 app/views/vips/exercises/MatrixChoiceTask/solve.php create mode 100644 app/views/vips/exercises/MatrixChoiceTask/xml.php create mode 100644 app/views/vips/exercises/MultipleChoiceTask/correct.php create mode 100644 app/views/vips/exercises/MultipleChoiceTask/edit.php create mode 100644 app/views/vips/exercises/MultipleChoiceTask/print.php create mode 100644 app/views/vips/exercises/MultipleChoiceTask/solve.php create mode 100644 app/views/vips/exercises/MultipleChoiceTask/xml.php create mode 100644 app/views/vips/exercises/SequenceTask/correct.php create mode 100644 app/views/vips/exercises/SequenceTask/edit.php create mode 100644 app/views/vips/exercises/SequenceTask/print.php create mode 100644 app/views/vips/exercises/SequenceTask/solve.php create mode 100644 app/views/vips/exercises/SequenceTask/xml.php create mode 100644 app/views/vips/exercises/SingleChoiceTask/correct.php create mode 100644 app/views/vips/exercises/SingleChoiceTask/edit.php create mode 100644 app/views/vips/exercises/SingleChoiceTask/print.php create mode 100644 app/views/vips/exercises/SingleChoiceTask/solve.php create mode 100644 app/views/vips/exercises/SingleChoiceTask/xml.php create mode 100644 app/views/vips/exercises/TextLineTask/correct.php create mode 100644 app/views/vips/exercises/TextLineTask/edit.php create mode 100644 app/views/vips/exercises/TextLineTask/print.php create mode 100644 app/views/vips/exercises/TextLineTask/solve.php create mode 100644 app/views/vips/exercises/TextLineTask/xml.php create mode 100644 app/views/vips/exercises/TextTask/correct.php create mode 100644 app/views/vips/exercises/TextTask/edit.php create mode 100644 app/views/vips/exercises/TextTask/print.php create mode 100644 app/views/vips/exercises/TextTask/solve.php create mode 100644 app/views/vips/exercises/TextTask/xml.php create mode 100644 app/views/vips/exercises/correct_exercise.php create mode 100644 app/views/vips/exercises/courseware_block.php create mode 100644 app/views/vips/exercises/evaluation_mode_info.php create mode 100644 app/views/vips/exercises/flexible_input.php create mode 100644 app/views/vips/exercises/flexible_textarea.php create mode 100644 app/views/vips/exercises/print_exercise.php create mode 100644 app/views/vips/exercises/show_exercise_files.php create mode 100644 app/views/vips/exercises/show_exercise_hint.php create mode 100644 app/views/vips/pool/assignments.php create mode 100644 app/views/vips/pool/copy_exercises_dialog.php create mode 100644 app/views/vips/pool/exercises.php create mode 100644 app/views/vips/pool/list_assignments.php create mode 100644 app/views/vips/pool/list_exercises.php create mode 100644 app/views/vips/pool/move_exercises_dialog.php create mode 100644 app/views/vips/sheets/add_exercise_dialog.php create mode 100644 app/views/vips/sheets/assign_block_dialog.php create mode 100644 app/views/vips/sheets/assignment_type_tooltip.php create mode 100644 app/views/vips/sheets/content_bar_icons.php create mode 100644 app/views/vips/sheets/copy_assignment_dialog.php create mode 100644 app/views/vips/sheets/copy_assignments_dialog.php create mode 100644 app/views/vips/sheets/copy_exercise_dialog.php create mode 100644 app/views/vips/sheets/copy_exercises_dialog.php create mode 100644 app/views/vips/sheets/edit_assignment.php create mode 100644 app/views/vips/sheets/edit_exercise.php create mode 100644 app/views/vips/sheets/export_assignment.php create mode 100644 app/views/vips/sheets/import_assignment_dialog.php create mode 100644 app/views/vips/sheets/ip_range_tooltip.php create mode 100644 app/views/vips/sheets/list_assignments.php create mode 100644 app/views/vips/sheets/list_assignments_list.php create mode 100644 app/views/vips/sheets/list_assignments_stud.php create mode 100644 app/views/vips/sheets/list_exercises.php create mode 100644 app/views/vips/sheets/move_assignments_dialog.php create mode 100644 app/views/vips/sheets/move_exercises_dialog.php create mode 100644 app/views/vips/sheets/print_assignment.php create mode 100644 app/views/vips/sheets/print_assignments.php create mode 100644 app/views/vips/sheets/print_layout.php create mode 100644 app/views/vips/sheets/show_assignment.php create mode 100644 app/views/vips/sheets/show_exercise.php create mode 100644 app/views/vips/sheets/show_exercise_link.php create mode 100644 app/views/vips/sheets/start_assignment_dialog.php create mode 100644 app/views/vips/solutions/assignment_solutions.php create mode 100644 app/views/vips/solutions/assignments.php create mode 100644 app/views/vips/solutions/assignments_list.php create mode 100644 app/views/vips/solutions/assignments_list_student.php create mode 100644 app/views/vips/solutions/autocorrect_dialog.php create mode 100644 app/views/vips/solutions/edit_assignment_attempt.php create mode 100644 app/views/vips/solutions/edit_group_dialog.php create mode 100644 app/views/vips/solutions/edit_solution.php create mode 100644 app/views/vips/solutions/feedback_files.php create mode 100644 app/views/vips/solutions/feedback_files_table.php create mode 100644 app/views/vips/solutions/gradebook_dialog.php create mode 100644 app/views/vips/solutions/participants_overview.php create mode 100644 app/views/vips/solutions/show_assignment_log.php create mode 100644 app/views/vips/solutions/solution_color_tooltip.php create mode 100644 app/views/vips/solutions/statistics.php create mode 100644 app/views/vips/solutions/student_assignment_solutions.php create mode 100644 app/views/vips/solutions/student_grade.php create mode 100644 app/views/vips/solutions/update_released_dialog.php create mode 100644 app/views/vips/solutions/view_solution.php create mode 100644 db/migrations/6.0.40_add_vips_module.php create mode 100644 lib/classes/sidebar/VipsSearchWidget.php create mode 100644 lib/filesystem/ExerciseFolder.php create mode 100644 lib/filesystem/FeedbackFolder.php create mode 100644 lib/filesystem/ResponseFolder.php create mode 100644 lib/models/Courseware/BlockTypes/TestBlock.php create mode 100644 lib/models/vips/ClozeTask.php create mode 100644 lib/models/vips/DummyExercise.php create mode 100644 lib/models/vips/Exercise.php create mode 100644 lib/models/vips/MatchingTask.php create mode 100644 lib/models/vips/MatrixChoiceTask.php create mode 100644 lib/models/vips/MultipleChoiceTask.php create mode 100644 lib/models/vips/SequenceTask.php create mode 100644 lib/models/vips/SingleChoiceTask.php create mode 100644 lib/models/vips/TextLineTask.php create mode 100644 lib/models/vips/TextTask.php create mode 100644 lib/models/vips/VipsAssignment.php create mode 100644 lib/models/vips/VipsAssignmentAttempt.php create mode 100644 lib/models/vips/VipsBlock.php create mode 100644 lib/models/vips/VipsExerciseRef.php create mode 100644 lib/models/vips/VipsGroup.php create mode 100644 lib/models/vips/VipsGroupMember.php create mode 100644 lib/models/vips/VipsSolution.php create mode 100644 lib/models/vips/VipsTest.php create mode 100644 lib/modules/VipsModule.php create mode 100644 public/assets/images/choice_checked.svg create mode 100644 public/assets/images/choice_unchecked.svg create mode 100644 public/assets/images/collapse.svg create mode 100644 public/assets/images/expand.svg create mode 100644 public/assets/images/icons/black/vips.svg create mode 100644 public/assets/images/icons/blue/assessment-mc.svg create mode 100644 public/assets/images/icons/blue/edit-line.svg create mode 100644 public/assets/images/icons/blue/vips.svg create mode 100644 public/assets/images/icons/red/vips.svg create mode 100644 public/assets/images/icons/white/vips.svg create mode 100644 public/assets/images/plus/screenshots/Vips/Vips_preview_1.png create mode 100644 public/assets/images/plus/screenshots/Vips/Vips_preview_2.png create mode 100644 public/assets/images/plus/screenshots/Vips/Vips_preview_3.png create mode 100644 resources/assets/javascripts/bootstrap/vips.js create mode 100644 resources/assets/javascripts/lib/vips.js create mode 100644 resources/assets/stylesheets/scss/vips.scss create mode 100644 resources/vue/components/courseware/blocks/CoursewareTestBlock.vue diff --git a/app/controllers/vips/admin.php b/app/controllers/vips/admin.php new file mode 100644 index 0000000..92c6cc7 --- /dev/null +++ b/app/controllers/vips/admin.php @@ -0,0 +1,208 @@ +range_id = Context::getId(); + } + + VipsModule::requireStatus('tutor', $block->range_id); + + $this->block = $block; + $this->groups = Statusgruppen::findBySeminar_id($block->range_id); + } + + /** + * Store changes to a block. + */ + public function store_block_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $block_id = Request::int('block_id'); + $group_id = Request::option('group_id'); + + if ($block_id) { + $block = VipsBlock::find($block_id); + } else { + $block = new VipsBlock(); + $block->range_id = Context::getId(); + } + + VipsModule::requireStatus('tutor', $block->range_id); + + $block->name = Request::get('block_name'); + $block->group_id = $group_id ?: null; + $block->visible = $group_id !== ''; + + if (!Request::int('block_grouped')) { + $block->weight = null; + } else if ($block->weight === null) { + $block->weight = 0; + + if ($block_id) { + // sum up individual assignment weights for total block weight + foreach (VipsAssignment::findByBlock_id($block_id) as $assignment) { + $block->weight += $assignment->weight; + } + } + } + + $block->store(); + + PageLayout::postSuccess(sprintf(_('Der Block „%s“ wurde gespeichert.'), htmlReady($block->name))); + + $this->redirect($this->url_for('vips/sheets', ['group' => 1])); + } + + /** + * Delete a block from the course. + */ + public function delete_block_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $block_id = Request::int('block_id'); + $block = VipsBlock::find($block_id); + $block_name = $block->name; + + VipsModule::requireStatus('tutor', $block->range_id); + + if ($block->delete()) { + PageLayout::postSuccess(sprintf(_('Der Block „%s“ wurde gelöscht.'), htmlReady($block_name))); + } + + $this->redirect('vips/sheets'); + } + + /** + * Stores the weights of blocks, sheets and exams + */ + public function store_weight_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_weight = Request::floatArray('assignment_weight'); + $block_weight = Request::floatArray('block_weight'); + + foreach ($assignment_weight as $assignment_id => $weight) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + $assignment->weight = $weight; + $assignment->store(); + } + + foreach ($block_weight as $block_id => $weight) { + $block = VipsBlock::find($block_id); + VipsModule::requireStatus('tutor', $block->range_id); + + $block->weight = $weight; + $block->store(); + } + + $this->redirect('vips/solutions'); + } + + /** + * Edit the grade distribution settings. + */ + public function edit_grades_action() + { + Navigation::activateItem('/course/vips/solutions'); + PageLayout::setHelpKeyword('Basis.VipsErgebnisse'); + + $course_id = Context::getId(); + VipsModule::requireStatus('tutor', $course_id); + + $grades = ['1,0', '1,3', '1,7', '2,0', '2,3', '2,7', '3,0', '3,3', '3,7', '4,0']; + $percentages = array_fill(0, count($grades), ''); + $comments = array_fill(0, count($grades), ''); + $settings = CourseConfig::get($course_id); + + foreach ($settings->VIPS_COURSE_GRADES as $value) { + $index = array_search($value['grade'], $grades); + + if ($index !== false) { + $percentages[$index] = $value['percent']; + $comments[$index] = $value['comment']; + } + } + + $this->grades = $grades; + $this->grade_settings = $settings->VIPS_COURSE_GRADES; + $this->percentages = $percentages; + $this->comments = $comments; + } + + /** + * Stores the distribution of grades + */ + public function store_grades_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $course_id = Context::getId(); + VipsModule::requireStatus('tutor', $course_id); + + $grades = ['1,0', '1,3', '1,7', '2,0', '2,3', '2,7', '3,0', '3,3', '3,7', '4,0']; + $percentages = Request::floatArray('percentage'); + $comments = Request::getArray('comment'); + $grade_settings = []; + $percent_last = 101; + $error = false; + + foreach ($percentages as $i => $percent) { + if ($percent) { + $grade_settings[] = [ + 'grade' => $grades[$i], + 'percent' => $percent, + 'comment' => trim($comments[$i]) + ]; + + if ($percent < 0 || $percent > 100) { + PageLayout::postError(_('Die Notenwerte müssen zwischen 0 und 100 liegen!')); + $error = true; + } else if ($percent_last <= $percent) { + PageLayout::postError(sprintf(_('Die Notenwerte müssen monoton absteigen (%s > %s)!'), $percent_last, $percent)); + $error = true; + } + + $percent_last = $percent; + } + } + + if (!$error) { + $settings = CourseConfig::get($course_id); + $settings->store('VIPS_COURSE_GRADES', $grade_settings); + + PageLayout::postSuccess(_('Die Notenwerte wurden eingetragen.')); + } + + $this->redirect('vips/solutions'); + } +} diff --git a/app/controllers/vips/api.php b/app/controllers/vips/api.php new file mode 100644 index 0000000..8c2dbe0 --- /dev/null +++ b/app/controllers/vips/api.php @@ -0,0 +1,256 @@ +id) { + VipsModule::requireStatus('tutor', $range_id); + } + + $assignments = VipsAssignment::findByRangeId($range_id); + + $data = []; + + foreach ($assignments as $assignment) { + if ($assignment->type !== 'exam') { + $data[] = [ + 'id' => (string) $assignment->id, + 'title' => $assignment->test->title, + 'type' => $assignment->type, + 'icon' => $assignment->getTypeIcon()->getShape(), + 'start' => date('d.m.Y, H:i', $assignment->start), + 'end' => date('d.m.Y, H:i', $assignment->end), + 'active' => $assignment->active, + 'block' => $assignment->block_id ? $assignment->block->name : null + ]; + } + } + + $this->render_json($data); + } + + public function assignment_action($assignment_id) + { + $assignment = VipsAssignment::find($assignment_id); + $user_id = $GLOBALS['user']->id; + + VipsModule::requireViewPermission($assignment); + + $released = $assignment->releaseStatus($user_id); + + if ($assignment->type === 'exam') { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf dieses Aufgabenblatt!')); + } + + if ( + !$assignment->checkAccess($user_id) + && $released < VipsAssignment::RELEASE_STATUS_CORRECTIONS + ) { + throw new AccessDeniedException(_('Das Aufgabenblatt kann zur Zeit nicht bearbeitet werden.')); + } + + // enter user start time the moment he/she first clicks on any exercise + if (!$assignment->checkEditPermission()) { + $assignment->recordAssignmentAttempt($user_id); + } + + $data = [ + 'id' => (string) $assignment->id, + 'title' => $assignment->test->title, + 'type' => $assignment->type, + 'icon' => $assignment->getTypeIcon()->getShape(), + 'start' => date('d.m.Y, H:i', $assignment->start), + 'end' => date('d.m.Y, H:i', $assignment->end), + 'active' => $assignment->active, + 'block' => $assignment->block_id ? $assignment->block->name : null, + 'reset_allowed' => $assignment->isRunning($user_id) && $assignment->isResetAllowed(), + 'points' => $assignment->test->getTotalPoints(), + 'release_status' => $released, + 'exercises' => [] + ]; + + foreach ($assignment->getExerciseRefs($user_id) as $exercise_ref) { + $template = $this->courseware_template($assignment, $exercise_ref, $released); + $exercise = $exercise_ref->exercise; + + $data['exercises'][] = [ + 'id' => $exercise->id, + 'type' => $exercise->type, + 'title' => $exercise->title, + 'template' => $template->render(), + 'item_count' => $exercise->itemCount(), + 'show_solution' => $template->show_solution + ]; + } + + $this->render_json($data); + } + + public function exercise_action($assignment_id, $exercise_id) + { + $assignment = VipsAssignment::find($assignment_id); + $user_id = $GLOBALS['user']->id; + + VipsModule::requireViewPermission($assignment, $exercise_id); + + $released = $assignment->releaseStatus($user_id); + + if ($assignment->type === 'exam') { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf dieses Aufgabenblatt!')); + } + + if ( + !$assignment->checkAccess($user_id) + && $released < VipsAssignment::RELEASE_STATUS_CORRECTIONS + ) { + throw new AccessDeniedException(_('Das Aufgabenblatt kann zur Zeit nicht bearbeitet werden.')); + } + + // enter user start time the moment he/she first clicks on any exercise + if (!$assignment->checkEditPermission()) { + $assignment->recordAssignmentAttempt($user_id); + } + + $exercise_ref = VipsExerciseRef::find([$assignment->test_id, $exercise_id]); + $template = $this->courseware_template($assignment, $exercise_ref, $released); + $exercise = $exercise_ref->exercise; + + $data = [ + 'id' => $exercise->id, + 'type' => $exercise->type, + 'title' => $exercise->title, + 'template' => $template->render(), + 'item_count' => $exercise->itemCount(), + 'show_solution' => $template->show_solution + ]; + + $this->render_json($data); + } + + private function courseware_template($assignment, $exercise_ref, $released) + { + $user_id = $GLOBALS['user']->id; + $exercise = $exercise_ref->exercise; + $solution = $assignment->getSolution($user_id, $exercise->id); + $max_tries = $assignment->getMaxTries(); + $max_points = $exercise_ref->points; + $sample_solution = false; + $show_solution = false; + $tries_left = null; + + if ($assignment->isRunning($user_id)) { + // if a solution has been submitted during a selftest + if ($max_tries && $solution) { + $tries_left = $max_tries - $solution->countTries(); + + if ( + $solution->points == $max_points + || !$solution->state + || $solution->grader_id + || $tries_left <= 0 + ) { + $show_solution = true; + $sample_solution = true; + } + } + } else { + $show_solution = true; + $sample_solution = $released == VipsAssignment::RELEASE_STATUS_SAMPLE_SOLUTIONS; + + if (!$solution) { + $solution = new VipsSolution(); + $solution->assignment = $assignment; + } + } + + $template = $this->get_template_factory()->open('vips/exercises/courseware_block'); + $template->user_id = $user_id; + $template->assignment = $assignment; + $template->exercise = $exercise; + $template->tries_left = $tries_left; + $template->solution = $solution; + $template->max_points = $max_points; + $template->sample_solution = $sample_solution; + $template->show_solution = $show_solution; + + return $template; + } + + public function solution_action($assignment_id, $exercise_id) + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment = VipsAssignment::find($assignment_id); + $block_id = Request::int('block_id'); + $user_id = $GLOBALS['user']->id; + + VipsModule::requireViewPermission($assignment, $exercise_id); + + // check access to courseware block + if ($block_id) { + $block = Courseware\Block::find($block_id); + $payload = $block->type->getPayload(); + + if ($payload['assignment'] != $assignment_id) { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf diesen Block!')); + } + } + + if ($assignment->type === 'exam') { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf dieses Aufgabenblatt!')); + } + + if (!$assignment->checkAccess($user_id)) { + throw new AccessDeniedException(_('Das Aufgabenblatt kann zur Zeit nicht bearbeitet werden.')); + } + + // enter user start time the moment he/she first clicks on any exercise + if (!$assignment->checkEditPermission()) { + $assignment->recordAssignmentAttempt($user_id); + } + + if (Request::isPost()) { + $request = Request::getInstance(); + $exercise = Exercise::find($exercise_id); + $solution = $exercise->getSolutionFromRequest($request, $_FILES); + $solution->user_id = $user_id; + + if ($solution->isEmpty()) { + $this->set_status(422); + } else { + $assignment->storeSolution($solution); + $this->set_status(201); + } + } + + if (Request::isDelete()) { + if ($assignment->isResetAllowed()) { + $assignment->deleteSolution($user_id, $exercise_id); + $this->set_status(204); + } else { + $this->set_status(403); + } + } + + // update user progress in Courseware + if ($block_id) { + $progress = new Courseware\UserProgress([$user_id, $block_id]); + $progress->grade = $assignment->getUserProgress($user_id); + $progress->store(); + } + + $this->render_nothing(); + } +} diff --git a/app/controllers/vips/config.php b/app/controllers/vips/config.php new file mode 100644 index 0000000..d6d4e48 --- /dev/null +++ b/app/controllers/vips/config.php @@ -0,0 +1,95 @@ +check('root'); + + Navigation::activateItem('/admin/config/vips'); + PageLayout::setHelpKeyword('Basis.VipsEinstellungen'); + PageLayout::setTitle(_('Einstellungen für Aufgaben')); + } + + public function index_action() + { + $this->fields = DataField::getDataFields('user'); + $this->config = Config::get(); + + $widget = new ActionsWidget(); + $widget->addLink( + _('Anstehende Klausuren anzeigen'), + $this->pending_assignmentsURL(), + Icon::create('doctoral_cap') + )->asDialog('size=big'); + Sidebar::get()->addWidget($widget); + } + + public function save_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exam_mode = Request::int('exam_mode', 0); + $exam_terms = trim(Request::get('exam_terms')); + $exam_terms = Studip\Markup::purifyHtml($exam_terms); + + $config = Config::get(); + $config->store('VIPS_EXAM_RESTRICTIONS', $exam_mode); + $config->store('VIPS_EXAM_TERMS', $exam_terms); + + $room = Request::getArray('room'); + $ip_range = Request::getArray('ip_range'); + $ip_ranges = []; + + foreach ($room as $i => $name) { + $name = preg_replace('/[ ,]+/', '_', trim($name)); + + if ($name !== '') { + $ip_ranges[$name] = trim($ip_range[$i]); + } + } + + if ($ip_ranges) { + ksort($ip_ranges); + $config->store('VIPS_EXAM_ROOMS', $ip_ranges); + } + + PageLayout::postSuccess(_('Die Einstellungen wurden gespeichert.')); + + $this->redirect('vips/config'); + } + + public function pending_assignments_action() + { + $this->assignments = VipsAssignment::findBySQL( + "range_type = 'course' AND type = 'exam' AND + start BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY) AND UNIX_TIMESTAMP(NOW() + INTERVAL 14 DAY) AND end > UNIX_TIMESTAMP() + ORDER BY start" + ); + } +} diff --git a/app/controllers/vips/exam_mode.php b/app/controllers/vips/exam_mode.php new file mode 100644 index 0000000..914a0e0 --- /dev/null +++ b/app/controllers/vips/exam_mode.php @@ -0,0 +1,29 @@ +addPlainText('', + _('Der normale Betrieb von Stud.IP ist für Sie zur Zeit gesperrt, da Klausuren geschrieben werden.')); + + $this->courses = VipsModule::getCoursesWithRunningExams($GLOBALS['user']->id); + } +} diff --git a/app/controllers/vips/pool.php b/app/controllers/vips/pool.php new file mode 100644 index 0000000..bcbe302 --- /dev/null +++ b/app/controllers/vips/pool.php @@ -0,0 +1,473 @@ +addPlainText('', + _('Auf dieser Seite finden Sie eine Übersicht über alle Aufgaben, auf die Sie Zugriff haben.')); + + $range_type = $_SESSION['view_context'] ?? 'user'; + $range_type = Request::option('range_type', $range_type); + $_SESSION['view_context'] = $range_type; + + $widget = new ViewsWidget(); + $widget->addLink( + _('Persönliche Aufgabensammlung'), + $this->url_for('vips/pool/exercises', ['range_type' => 'user']) + )->setActive($range_type === 'user'); + $widget->addLink( + _('Aufgaben in Veranstaltungen'), + $this->url_for('vips/pool/exercises', ['range_type' => 'course']) + )->setActive($range_type === 'course'); + Sidebar::get()->addWidget($widget); + + $sort = Request::option('sort', 'mkdate'); + $desc = Request::int('desc', $sort === 'mkdate'); + $page = Request::int('page', 1); + $size = Config::get()->ENTRIES_PER_PAGE; + + $search_filter = Request::getArray('search_filter') + ['search_string' => '', 'exercise_type' => '']; + $search_filter['search_string'] = Request::get('pool_search_parameter', $search_filter['search_string']); + $search_filter['exercise_type'] = Request::get('exercise_type', $search_filter['exercise_type']); + + if (Request::submitted('start_search') || Request::int('pool_search')) { + $search_filter = [ + 'search_string' => Request::get('pool_search_parameter'), + 'exercise_type' => Request::get('exercise_type') + ]; + } else if (empty($search_filter) || Request::submitted('reset_search')) { + $search_filter = array_fill_keys(['search_string', 'exercise_type'], ''); + } + + // get exercises of this user and where he/she has permission + if ($range_type === 'course') { + $course_ids = array_column(VipsModule::getActiveCourses($GLOBALS['user']->id), 'id'); + } else { + $course_ids = [$GLOBALS['user']->id]; + } + + // set up the sql query for the quicksearch + $sql = "SELECT etask_tasks.id, etask_tasks.title FROM etask_tasks + JOIN etask_test_tasks ON etask_tasks.id = etask_test_tasks.task_id + JOIN etask_assignments USING (test_id) + WHERE etask_assignments.range_id IN ('" . implode("','", $course_ids) . "') + AND etask_assignments.type IN ('exam', 'practice', 'selftest') + AND (etask_tasks.title LIKE :input OR etask_tasks.description LIKE :input) + AND IF(:exercise_type = '', 1, etask_tasks.type = :exercise_type) + ORDER BY title"; + $search = new SQLSearch($sql, _('Titel der Aufgabe')); + + $widget = new VipsSearchWidget($this->url_for('vips/pool/exercises', ['exercise_type' => $search_filter['exercise_type']])); + $widget->addNeedle(_('Suche'), 'pool_search', true, $search, 'function(id, name) { this.value = name; this.form.submit(); }', $search_filter['search_string']); + Sidebar::get()->addWidget($widget); + + $widget = new SelectWidget(_('Aufgabentyp'), $this->url_for('vips/pool/exercises', ['pool_search_parameter' => $search_filter['search_string']]), 'exercise_type'); + $element = new SelectElement('', _('Alle Aufgabentypen')); + $widget->addElement($element); + Sidebar::get()->addWidget($widget); + + foreach (Exercise::getExerciseTypes() as $type => $entry) { + $element = new SelectElement($type, $entry['name'], $type === $search_filter['exercise_type']); + $widget->addElement($element); + } + + $result = $this->getAllExercises($course_ids, $sort, $desc, $search_filter); + + $this->sort = $sort; + $this->desc = $desc; + $this->page = $page; + $this->count = count($result); + $this->exercises = array_slice($result, $size * ($page - 1), $size); + $this->search_filter = $search_filter; + } + + /** + * Display all assignments that are available for this user. + * Available in this case means the assignment is in a course where the user + * is at least tutor. + * Lecturer/tutor can select which assignment to edit/delete. + */ + public function assignments_action() + { + Navigation::activateItem('/contents/vips/assignments'); + PageLayout::setTitle(_('Meine Aufgabenblätter')); + + Helpbar::get()->addPlainText('', + _('Auf dieser Seite finden Sie eine Übersicht über alle Aufgabenblätter, auf die Sie Zugriff haben.')); + + $range_type = $_SESSION['view_context'] ?? 'user'; + $range_type = Request::option('range_type', $range_type); + $_SESSION['view_context'] = $range_type; + + $widget = new ActionsWidget(); + $widget->addLink( + _('Aufgabenblatt erstellen'), + $this->url_for('vips/sheets/edit_assignment'), + Icon::create('add') + ); + $widget->addLink( + _('Aufgabenblatt kopieren'), + $this->url_for('vips/sheets/copy_assignment_dialog'), + Icon::create('copy') + )->asDialog('size=1200x800'); + $widget->addLink( + _('Aufgabenblatt importieren'), + $this->url_for('vips/sheets/import_assignment_dialog'), + Icon::create('import') + )->asDialog('size=auto'); + Sidebar::get()->addWidget($widget); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Persönliche Aufgabensammlung'), + $this->url_for('vips/pool/assignments', ['range_type' => 'user']) + )->setActive($range_type === 'user'); + $widget->addLink( + _('Aufgaben in Veranstaltungen'), + $this->url_for('vips/pool/assignments', ['range_type' => 'course']) + )->setActive($range_type === 'course'); + Sidebar::get()->addWidget($widget); + + $sort = Request::option('sort', 'mkdate'); + $desc = Request::int('desc', $sort === 'mkdate'); + $page = Request::int('page', 1); + $size = Config::get()->ENTRIES_PER_PAGE; + + $search_filter = Request::getArray('search_filter') + ['search_string' => '', 'assignment_type' => '']; + $search_filter['search_string'] = Request::get('pool_search_parameter', $search_filter['search_string']); + $search_filter['assignment_type'] = Request::get('assignment_type', $search_filter['assignment_type']); + + // get assignments of this user and where he/she has permission + if ($range_type === 'course') { + $course_ids = array_column(VipsModule::getActiveCourses($GLOBALS['user']->id), 'id'); + } else { + $course_ids = [$GLOBALS['user']->id]; + } + + // set up the sql query for the quicksearch + $sql = "SELECT etask_assignments.id, etask_tests.title FROM etask_tests + JOIN etask_assignments ON etask_tests.id = etask_assignments.test_id + WHERE etask_assignments.range_id IN ('" . implode("','", $course_ids) . "') + AND etask_assignments.type IN ('exam', 'practice', 'selftest') + AND (etask_tests.title LIKE :input OR etask_tests.description LIKE :input) + AND IF(:assignment_type = '', 1, etask_assignments.type = :assignment_type) + ORDER BY title"; + $search = new SQLSearch($sql, _('Titel des Aufgabenblatts')); + + $widget = new VipsSearchWidget($this->url_for('vips/pool/assignments', ['assignment_type' => $search_filter['assignment_type']])); + $widget->addNeedle(_('Suche'), 'pool_search', true, $search, 'function(id, name) { this.value = name; this.form.submit(); }', $search_filter['search_string']); + Sidebar::get()->addWidget($widget); + + $widget = new SelectWidget(_('Modus'), $this->url_for('vips/pool/assignments', ['pool_search_parameter' => $search_filter['search_string']]), 'assignment_type'); + $element = new SelectElement('', _('Beliebiger Modus')); + $widget->addElement($element); + Sidebar::get()->addWidget($widget); + + foreach (VipsAssignment::getAssignmentTypes() as $type => $entry) { + $element = new SelectElement($type, $entry['name'], $type === $search_filter['assignment_type']); + $widget->addElement($element); + } + + $result = $this->getAllAssignments($course_ids, $sort, $desc, $search_filter); + + $this->sort = $sort; + $this->desc = $desc; + $this->page = $page; + $this->count = count($result); + $this->assignments = array_slice($result, $size * ($page - 1), $size); + $this->search_filter = $search_filter; + } + + /** + * Get all matching exercises from a list of courses in given order. + * If $search_filter is not empty, search filters are applied. + * + * @param course_ids list of courses to get exercises from + * @param sort sort exercise list by this property + * @param desc true if sort direction is descending + * @param search_filter the currently active search filter + * + * @return array with data of all matching exercises + */ + public function getAllExercises($course_ids, $sort, $desc, $search_filter) + { + $db = DBManager::get(); + + // check if some filters are active + $search_string = $search_filter['search_string']; + $exercise_type = $search_filter['exercise_type']; + + $sql = "SELECT etask_tasks.*, + auth_user_md5.Nachname, + auth_user_md5.Vorname, + etask_assignments.id AS assignment_id, + etask_assignments.range_id, + etask_assignments.range_type, + etask_tests.title AS test_title + FROM etask_tasks + LEFT JOIN auth_user_md5 USING(user_id) + JOIN etask_test_tasks ON etask_tasks.id = etask_test_tasks.task_id + JOIN etask_tests ON etask_tests.id = etask_test_tasks.test_id + JOIN etask_assignments USING (test_id) + WHERE etask_assignments.range_id IN (:course_ids) + AND etask_assignments.type IN ('exam', 'practice', 'selftest') " . + ($search_string ? 'AND (etask_tasks.title LIKE :input OR + etask_tasks.description LIKE :input) ' : '') . + ($exercise_type ? 'AND etask_tasks.type = :exercise_type ' : '') . + "ORDER BY :sort :desc, title"; + + $stmt = $db->prepare($sql); + $stmt->bindValue(':course_ids', $course_ids); + $stmt->bindValue(':input', '%' . $search_string . '%'); + $stmt->bindValue(':exercise_type', $exercise_type); + $stmt->bindValue(':sort', $sort, StudipPDO::PARAM_COLUMN); + $stmt->bindValue(':desc', $desc ? 'DESC' : 'ASC', StudipPDO::PARAM_COLUMN); + $stmt->execute(); + + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + + /** + * Get all matching assignments from a list of courses in given order. + * If $search_filter is not empty, search filters are applied. + * + * @param course_ids list of courses to get assignments from + * @param sort sort assignment list by this property + * @param desc true if sort direction is descending + * @param search_filter the currently active search filter + * + * @return array with data of all matching assignments + */ + public function getAllAssignments($course_ids, $sort, $desc, $search_filter) + { + $db = DBManager::get(); + + // check if some filters are active + $search_string = $search_filter['search_string']; + $assignment_type = $search_filter['assignment_type']; + $types = $assignment_type ? [$assignment_type] : ['exam', 'practice', 'selftest']; + + $sql = "SELECT etask_assignments.*, + etask_tests.title AS test_title, + auth_user_md5.Nachname, + auth_user_md5.Vorname, + seminare.Name, + (SELECT MIN(beginn) FROM semester_data + JOIN semester_courses USING(semester_id) + WHERE course_id = Seminar_id) AS start_time + FROM etask_tests + LEFT JOIN auth_user_md5 USING(user_id) + JOIN etask_assignments ON etask_tests.id = etask_assignments.test_id + LEFT JOIN seminare ON etask_assignments.range_id = seminare.Seminar_id + WHERE etask_assignments.range_id IN (:course_ids) + AND etask_assignments.type IN (:types) " . + ($search_string ? 'AND (etask_tests.title LIKE :input OR + etask_tests.description LIKE :input) ' : '') . + "ORDER BY :sort :desc, title"; + + $stmt = $db->prepare($sql); + $stmt->bindValue(':course_ids', $course_ids); + $stmt->bindValue(':input', '%' . $search_string . '%'); + $stmt->bindValue(':types', $types); + $stmt->bindValue(':sort', $sort, StudipPDO::PARAM_COLUMN); + $stmt->bindValue(':desc', $desc ? 'DESC' : 'ASC', StudipPDO::PARAM_COLUMN); + $stmt->execute(); + + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + + /** + * Delete a list of exercises from their respective assignments. + */ + public function delete_exercises_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_ids = Request::intArray('exercise_ids'); + $deleted = 0; + + foreach ($exercise_ids as $exercise_id => $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment, $exercise_id); + + if (!$assignment->isLocked()) { + $assignment->test->removeExercise($exercise_id); + ++$deleted; + } + } + + if ($deleted > 0) { + PageLayout::postSuccess(sprintf(ngettext('Die Aufgabe wurde gelöscht.', 'Es wurden %s Aufgaben gelöscht.', $deleted), $deleted)); + } + + if ($deleted < count($exercise_ids)) { + PageLayout::postError(_('Einige Aufgaben konnten nicht gelöscht werden, da die Aufgabenblätter gesperrt sind.'), [ + _('Falls Sie diese wirklich löschen möchten, müssen Sie zuerst die Lösungen aller Teilnehmenden zurücksetzen.') + ]); + } + + $this->redirect('vips/pool/exercises'); + } + + /** + * Dialog for copying a list of exercises into an existing assignment. + */ + public function copy_exercises_dialog_action() + { + PageLayout::setTitle(_('Aufgaben in vorhandenes Aufgabenblatt kopieren')); + + $this->exercise_ids = Request::intArray('exercise_ids'); + $this->courses = VipsModule::getActiveCourses($GLOBALS['user']->id); + } + + /** + * Copy the selected exercises into the selected assignment. + */ + public function copy_exercises_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_ids = Request::intArray('exercise_ids'); + $target_assignment_id = Request::int('assignment_id'); + $target_assignment = VipsAssignment::find($target_assignment_id); + + VipsModule::requireEditPermission($target_assignment); + + if (!$target_assignment->isLocked()) { + foreach ($exercise_ids as $exercise_id => $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + $exercise_ref = VipsExerciseRef::find([$assignment->test_id, $exercise_id]); + $exercise_ref->copyIntoTest($target_assignment->test_id); + } + + PageLayout::postSuccess(ngettext('Die Aufgabe wurde kopiert.', 'Die Aufgaben wurden kopiert.', count($exercise_ids))); + } + + $this->redirect('vips/pool/exercises'); + } + + /** + * Dialog for moving a list of exercises into an existing assignment. + */ + public function move_exercises_dialog_action() + { + PageLayout::setTitle(_('Aufgaben in vorhandenes Aufgabenblatt verschieben')); + + $this->exercise_ids = Request::intArray('exercise_ids'); + $this->courses = VipsModule::getActiveCourses($GLOBALS['user']->id); + } + + /** + * Move the selected exercises into the selected assignment. + */ + public function move_exercises_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_ids = Request::intArray('exercise_ids'); + $target_assignment_id = Request::int('assignment_id'); + $target_assignment = VipsAssignment::find($target_assignment_id); + $moved = 0; + + VipsModule::requireEditPermission($target_assignment); + + if (!$target_assignment->isLocked()) { + foreach ($exercise_ids as $exercise_id => $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + if (!$assignment->isLocked()) { + $exercise_ref = VipsExerciseRef::find([$assignment->test_id, $exercise_id]); + $exercise_ref->moveIntoTest($target_assignment->test_id); + ++$moved; + } + } + } + + if ($moved > 0) { + PageLayout::postSuccess(sprintf(ngettext('Die Aufgabe wurde verschoben.', 'Es wurden %s Aufgaben verschoben.', $moved), $moved)); + } + + if ($moved < count($exercise_ids)) { + PageLayout::postError(_('Einige Aufgaben konnten nicht verschoben werden, da die Aufgabenblätter gesperrt sind.')); + } + + $this->redirect('vips/pool/exercises'); + } + + /** + * Return the appropriate CSS class for sortable column (if any). + * + * @param boolean $sort sort by this column + * @param boolean $desc set sort direction + */ + public function sort_class(bool $sort, ?bool $desc): string + { + return $sort ? ($desc ? 'sortdesc' : 'sortasc') : ''; + } + + /** + * Render a generic page chooser selector. The first occurence of '%d' + * in the URL is replaced with the selected page number. + * + * @param string $url URL for one of the pages + * @param string $count total number of entries + * @param string $page current page to display + * @param string|null $dialog Optional dialog attribute content + * @param int|null $page_size page size (defaults to system default) + * @return mixed + */ + function page_chooser(string $url, string $count, string $page, ?string $dialog = null, ?int $page_size = null) + { + $template = $GLOBALS['template_factory']->open('shared/pagechooser'); + $template->dialog = $dialog; + $template->num_postings = $count; + $template->page = $page; + $template->perPage = $page_size ?: Config::get()->ENTRIES_PER_PAGE; + $template->pagelink = str_replace('%%25d', '%d', str_replace('%', '%%', $url)); + + return $template->render(); + } +} diff --git a/app/controllers/vips/sheets.php b/app/controllers/vips/sheets.php new file mode 100644 index 0000000..036ff10 --- /dev/null +++ b/app/controllers/vips/sheets.php @@ -0,0 +1,2305 @@ +id); + + VipsModule::requireViewPermission($assignment, $exercise_id); + + if (!$assignment->checkEditPermission()) { + $solver_id = $GLOBALS['user']->id; + } + + $solutions = $assignment->getArchivedUserSolutions($solver_id, $exercise_id); + + if ($assignment->checkAccess($solver_id) || $assignment->checkEditPermission()) { + if ($assignment->type === 'exam' && $solutions) { + $assignment->restoreSolution($solutions[0]); + PageLayout::postSuccess(_('Die vorherige Lösung wurde wiederhergestellt.')); + } + } + + $this->redirect($this->url_for('vips/sheets/show_exercise', compact('assignment_id', 'exercise_id', 'solver_id'))); + } + + /** + * Only possible if test is selftest: Delete the solution of a student for + * a particular exercise. + */ + public function delete_solution_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_id = Request::int('exercise_id'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id', $GLOBALS['user']->id); + + VipsModule::requireViewPermission($assignment, $exercise_id); + + if (!$assignment->checkEditPermission()) { + $solver_id = $GLOBALS['user']->id; + } + + if ($assignment->checkAccess($solver_id) || $assignment->checkEditPermission()) { + if ($assignment->isResetAllowed() || $assignment->type === 'exam') { + $assignment->deleteSolution($solver_id, $exercise_id); + $undo_link = ''; + + if ($assignment->type === 'exam' && !$assignment->isSelfAssessment()) { + $undo_link = sprintf(' %s', + $this->link_for('vips/sheets/restore_solution', compact('assignment_id', 'exercise_id', 'solver_id')), + _('Diese Aktion zurücknehmen.')); + } + + PageLayout::postSuccess(_('Die Lösung wurde gelöscht.') . $undo_link); + } + } + + $this->redirect($this->url_for('vips/sheets/show_exercise', compact('assignment_id', 'exercise_id', 'solver_id'))); + } + + /** + * Only possible if test is selftest: Deletes all the solutions of a student or + * the student's group to enable him/her to redo it. + */ + public function delete_solutions_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id', $GLOBALS['user']->id); + + VipsModule::requireViewPermission($assignment); + + if (!$assignment->checkEditPermission()) { + $solver_id = $GLOBALS['user']->id; + } + + if ($assignment->isRunning() || $assignment->checkEditPermission()) { + if ($assignment->isResetAllowed()) { + $assignment->deleteSolutions($solver_id); + PageLayout::postSuccess(_('Die Lösungen wurden gelöscht.')); + } + } + + $this->redirect($this->url_for('vips/sheets/show_assignment', compact('assignment_id', 'solver_id'))); + } + + /** + * Only possible if test is exam: Begin working on the exam. + */ + public function begin_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $terms_accepted = Request::int('terms_accepted'); + $access_code = Request::get('access_code'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $ip_address = $_SERVER['REMOTE_ADDR']; + + VipsModule::requireViewPermission($assignment); + + if ($assignment->type === 'exam') { + if (!$assignment->getAssignmentAttempt($GLOBALS['user']->id)) { + $exam_terms = Config::get()->VIPS_EXAM_TERMS; + } + + if (!$assignment->isRunning() || !$assignment->active) { + PageLayout::postError(_('Das Aufgabenblatt kann zur Zeit nicht bearbeitet werden.')); + } else if (!$assignment->checkIPAccess($ip_address)) { + PageLayout::postError(sprintf(_('Sie haben mit Ihrer IP-Adresse „%s“ keinen Zugriff!'), htmlReady($ip_address))); + } else if ($exam_terms && !$terms_accepted) { + PageLayout::postError(_('Ein Start der Klausur ist nur mit Bestätigung der Teilnahmebedingungen möglich.')); + } else if (!$assignment->checkAccessCode($access_code)) { + PageLayout::postError(_('Der eingegebene Zugangscode ist nicht korrekt.')); + } else { + $assignment->recordAssignmentAttempt($GLOBALS['user']->id); + } + } + + $this->redirect($this->url_for('vips/sheets/show_assignment', compact('assignment_id', 'access_code'))); + } + + /** + * Only possible if test is exam: Immediately finish working on the exam. + */ + public function finish_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireViewPermission($assignment); + + if ($assignment->checkAccess($GLOBALS['user']->id)) { + if ($assignment->finishAssignmentAttempt($GLOBALS['user']->id)) { + PageLayout::postSuccess(_('Das Aufgabenblatt wurde abgeschlossen, eine weitere Bearbeitung ist nicht mehr möglich.')); + } else { + PageLayout::postError(_('Eine Abgabe ist erst nach Start des Aufgabenblatts möglich.')); + } + } + + $this->redirect($this->url_for('vips/sheets/show_assignment', compact('assignment_id'))); + } + + /** + * SHEETS/EXAMS + * + * Is called when the submit button at the bottom of an exercise is called. + * If there is already a solution of this exercise by the same user or same group, + * a dialog pops up to confirm the submission. On database-level: EVERY solution is stored + * (even the unconfirmed ones), with the last solution being marked as last. + */ + public function submit_exercise_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_id = Request::int('exercise_id'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireViewPermission($assignment, $exercise_id); + + ################################################################## + # in case student solution is submitted by tutor or lecturer # + # (can happen if the student submits his/her solution by email) # + ################################################################## + + $solver_id = Request::option('solver_id'); + + if ($solver_id == '' || !$assignment->checkEditPermission()) { + $solver_id = $GLOBALS['user']->id; + } + + ############################ + # Checks before submission # + ############################ + + if (!$assignment->checkEditPermission()) { + $end = $assignment->getUserEndTime($solver_id); + + // not yet started + if (!$assignment->isStarted()) { + PageLayout::postError(_('Das Aufgabenblatt wurde noch nicht gestartet.')); + $this->redirect('vips/sheets/list_assignments_stud'); + return; + } + + // already ended + if ($end && time() - $end > 120) { + PageLayout::postError(_('Das Aufgabenblatt wurde bereits beendet.')); + $this->redirect('vips/sheets/list_assignments_stud'); + return; + } + + if (!$assignment->checkIPAccess($_SERVER['REMOTE_ADDR']) || !$assignment->checkAccessCode()) { + PageLayout::postError(_('Kein Zugriff möglich!')); + $this->redirect('vips/sheets/list_assignments_stud'); + return; + } + + $assignment->recordAssignmentAttempt($solver_id); + } + + /* if an exercise has been submitted */ + if (Request::submitted('submit_exercise') || Request::int('forced')) { + $request = Request::getInstance(); + $exercise = Exercise::find($exercise_id); + $solution = $exercise->getSolutionFromRequest($request, $_FILES); + $solution->user_id = $solver_id; + + if ($solution->isEmpty()) { + PageLayout::postWarning(_('Ihre Lösung ist leer und wurde nicht gespeichert.')); + } else { + $assignment->storeSolution($solution); + + PageLayout::postSuccess(sprintf(_('Ihre Lösung zur Aufgabe „%s“ wurde gespeichert.'), htmlReady($exercise->title))); + } + } + + $this->redirect($this->url_for('vips/sheets/show_exercise', compact('assignment_id', 'exercise_id', 'solver_id'))); + } + + /** + * SHEETS/EXAMS + * + * Displays an exercise (from student perspective) + */ + public function show_exercise_action() + { + $exercise_id = Request::int('exercise_id'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id'); // solver is handed over via address line, ie. user is a lecturer + + VipsModule::requireViewPermission($assignment, $exercise_id); + + if ($solver_id == '' || !$assignment->checkEditPermission()) { + $solver_id = $GLOBALS['user']->id; + } + + ############################################################## + # check for ip_address, remaining time and interrupted # + ############################################################## + + // restrict access for students! + if (!$assignment->checkEditPermission()) { + // the assignment is not accessible any more after it has run out + if (!$assignment->checkAccess()) { + PageLayout::postError(_('Das Aufgabenblatt kann zur Zeit nicht bearbeitet werden.')); + $this->redirect('vips/sheets/list_assignments_stud'); + return; + } + + if ($assignment->isFinished($solver_id)) { + PageLayout::postError(_('Die Zeit ist leider abgelaufen!')); + $this->redirect($this->url_for('vips/sheets/show_assignment', compact('assignment_id'))); + return; + } + + // enter user start time the moment he/she first clicks on any exercise + $assignment->recordAssignmentAttempt($solver_id); + } + + // fetch exercise info, type, points + $exercise_ref = VipsExerciseRef::find([$assignment->test_id, $exercise_id]); + $exercise = $exercise_ref->exercise; + + ################################### + # get user solution if applicable # + ################################### + + $solution = $assignment->getSolution($solver_id, $exercise_id); + $max_tries = $assignment->getMaxTries(); + $max_points = $exercise_ref->points; + $exercise_position = $exercise_ref->position; + $show_solution = false; + $tries_left = null; + + // if a solution has been submitted during a selftest + if ($max_tries && $solution) { + $tries_left = $max_tries - $solution->countTries(); + + if ($solution->points == $max_points || !$solution->state || $solution->grader_id || $tries_left <= 0) { + $show_solution = true; + } + } + + ############################## + # set template variables # + ############################## + + $this->assignment = $assignment; + $this->assignment_id = $assignment_id; + $this->exercise = $exercise; + $this->exercise_id = $exercise_id; + $this->exercise_position = $exercise_position; + + $this->solver_id = $solver_id; + $this->solution = $solution; // can be empty + $this->max_points = $max_points; + $this->show_solution = $show_solution; + $this->tries_left = $tries_left; + $this->user_end_time = $assignment->getUserEndTime($solver_id); + $this->remaining_time = $this->user_end_time - time(); + + $this->contentbar = $this->create_contentbar($assignment, $exercise_id, 'show'); + + $widget = new ActionsWidget(); + + if (($assignment->isResetAllowed() || $assignment->type === 'exam') && $solution) { + $widget->addLink( + _('Lösung dieser Aufgabe löschen'), + $this->url_for('vips/sheets/delete_solution', compact('assignment_id', 'exercise_id', 'solver_id')), + Icon::create('refresh'), + ['data-confirm' => _('Wollen Sie die Lösung dieser Aufgabe wirklich löschen?')] + )->asButton(); + } + + Sidebar::get()->addWidget($widget); + + if ($assignment->checkEditPermission()) { + Helpbar::get()->addPlainText('', + _('Dies ist die Studierendenansicht (Vorschau) der Aufgabe. Sie können hier auch Lösungen von Teilnehmenden ansehen oder für sie abgeben.')); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Aufgabe bearbeiten'), + $this->url_for('vips/sheets/edit_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $exercise_id]) + ); + $widget->addLink( + _('Studierendensicht (Vorschau)'), + $this->url_for('vips/sheets/show_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $exercise_id]) + )->setActive(); + Sidebar::get()->addWidget($widget); + + if ($assignment->range_type === 'course') { + $widget = new SelectWidget(_('Anzeigen für'), $this->url_for('vips/sheets/show_exercise', compact('assignment_id', 'exercise_id')), 'solver_id'); + $widget->class = 'nested-select'; + $element = new SelectElement($GLOBALS['user']->id, ' ', $GLOBALS['user']->id == $solver_id); + $widget->addElement($element); + + foreach ($assignment->course->members->findBy('status', 'autor')->orderBy('nachname, vorname') as $member) { + if ($assignment->isVisible($member->user_id)) { + $element = new SelectElement($member->user_id, $member->nachname . ', ' . $member->vorname, $member->user_id == $solver_id); + $widget->addElement($element); + } + } + Sidebar::get()->addWidget($widget); + } + } else { + Helpbar::get()->addPlainText('', + _('Bitte denken Sie daran, vor dem Verlassen der Seite Ihre Lösung zu speichern.')); + } + + $widget = new ViewsWidget(); + $widget->setTitle(_('Aufgabenblatt')); + + setlocale(LC_NUMERIC, $_SESSION['_language'] . '.UTF-8'); + + foreach ($assignment->getExerciseRefs($solver_id) as $i => $item) { + $this->item = $item; + $this->position = $i + 1; + $element = new WidgetElement($this->render_template_as_string('vips/sheets/show_exercise_link')); + $element->active = $item->task_id === $exercise->id; + $widget->addElement($element, 'exercise-' . $item->task_id); + } + + setlocale(LC_NUMERIC, 'C'); + + Sidebar::get()->addWidget($widget); + } + + /** + * Displays all running assignments "work-on ready" for students (view of + * students when clicking on tab Uebungsblatt), respectively student view + * for lecturers and tutors. + */ + public function list_assignments_stud_action() + { + $course_id = Context::getId(); + $sort = Request::option('sort', 'start'); + $desc = Request::int('desc'); + VipsModule::requireStatus('autor', $course_id); + + $this->sort = $sort; + $this->desc = $desc; + $this->assignments = []; + + $assignments = VipsAssignment::findByRangeId($course_id); + $blocks = VipsBlock::findBySQL('range_id = ? ORDER BY name', [$course_id]); + $blocks[] = VipsBlock::build(['name' => _('Aufgabenblätter')]); + $ip_address = $_SERVER['REMOTE_ADDR']; + + usort($assignments, function($a, $b) use ($sort) { + if ($sort === 'title') { + return strcoll($a->test->title, $b->test->title); + } else if ($sort === 'type') { + return strcmp($a->type, $b->type); + } else if ($sort === 'start') { + return strcmp($a->start, $b->start); + } else { + return strcmp($a->end ?: '~', $b->end ?: '~'); + } + }); + + if ($desc) { + $assignments = array_reverse($assignments); + } + + foreach ($blocks as $block) { + $this->blocks[$block->id]['title'] = $block->name; + } + + foreach ($assignments as $assignment) { + if ($assignment->isRunning() && $assignment->isVisible($GLOBALS['user']->id)) { + if ($assignment->checkIPAccess($ip_address)) { + if (isset($assignment->block->group_id)) { + $this->blocks['']['assignments'][] = $assignment; + } else { + $this->blocks[$assignment->block_id]['assignments'][] = $assignment; + } + } + } + } + + // delete empty blocks + foreach ($blocks as $block) { + if (empty($this->blocks[$block->id]['assignments'])) { + unset($this->blocks[$block->id]); + } + } + + $this->user_id = $GLOBALS['user']->id; + } + + /** + * Display one assignment to the student, including the list of exercises. + */ + public function show_assignment_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id', $GLOBALS['user']->id); + $ip_address = $_SERVER['REMOTE_ADDR']; + + VipsModule::requireViewPermission($assignment); + + if (!$assignment->checkEditPermission()) { + $solver_id = $GLOBALS['user']->id; + } + + $this->solver_id = $solver_id; + $this->user_end_time = $assignment->getUserEndTime($solver_id); + $this->remaining_time = $this->user_end_time - time(); + $this->access_code = trim(Request::get('access_code')); + $this->assignment = $assignment; + $this->needs_code = false; + $this->exam_terms = null; + $this->preview_exam_terms = null; + + $this->contentbar = $this->create_contentbar($assignment, null, 'show'); + + if (!$assignment->checkEditPermission()) { + if (!$assignment->isRunning() || !$assignment->active) { + PageLayout::postError(_('Das Aufgabenblatt kann zur Zeit nicht bearbeitet werden.')); + $this->redirect('vips/sheets/list_assignments_stud'); + return; + } + + if (!$assignment->checkIPAccess($ip_address)) { + PageLayout::postError(sprintf(_('Sie haben mit Ihrer IP-Adresse „%s“ keinen Zugriff!'), htmlReady($ip_address))); + $this->redirect('vips/sheets/list_assignments_stud'); + return; + } + + $this->assignment_attempt = $assignment->getAssignmentAttempt($solver_id); + + if ($assignment->type === 'exam') { + if (!$assignment->checkAccessCode()) { + $this->needs_code = true; + } + + if (!$this->assignment_attempt) { + $this->exam_terms = Config::get()->VIPS_EXAM_TERMS; + } + + if ($this->exam_terms || $this->needs_code) { + $this->contentbar = $this->contentbar->withProps(['toc' => null]); + } + } + + $widget = new ActionsWidget(); + + if ($assignment->type !== 'exam') { + $widget->addLink( + _('Aufgabenblatt drucken'), + $this->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment_id, 'print_files' => 1]), + Icon::create('print'), + ['target' => '_blank'] + ); + } + if ($assignment->isResetAllowed()) { + $widget->addLink( + _('Lösungen dieses Blatts löschen'), + $this->url_for('vips/sheets/delete_solutions', ['assignment_id' => $assignment_id]), + Icon::create('refresh'), + ['data-confirm' => _('Wollen Sie die Lösungen dieses Aufgabenblatts wirklich löschen?')] + )->asButton(); + } + if ($assignment->type === 'exam' && $this->assignment_attempt && $this->remaining_time > 0) { + $widget->addLink( + _('Klausur vorzeitig abgeben'), + $this->url_for('vips/sheets/finish_assignment', ['assignment_id' => $assignment_id]), + Icon::create('lock-locked'), + ['data-confirm' => _('Achtung: Wenn Sie die Klausur abgeben, sind keine weiteren Eingaben mehr möglich!')] + )->asButton(); + } + if ($assignment->type === 'selftest' && $this->assignment_attempt && $this->assignment_attempt->end === null) { + $widget->addLink( + _('Aufgabenblatt jetzt abgeben'), + $this->url_for('vips/sheets/finish_assignment', ['assignment_id' => $assignment_id]), + Icon::create('lock-locked'), + ['data-confirm' => _('Achtung: Wenn Sie das Aufgabenblatt abgeben, sind keine weiteren Eingaben mehr möglich!')] + )->asButton(); + } + Sidebar::get()->addWidget($widget); + } else { + if ($assignment->type === 'exam') { + $this->preview_exam_terms = Config::get()->VIPS_EXAM_TERMS; + } + + Helpbar::get()->addPlainText('', + _('Dies ist die Studierendensicht (Vorschau) des Aufgabenblatts.')); + + $widget = new ActionsWidget(); + + if ($assignment->type !== 'exam') { + $widget->addLink( + _('Aufgabenblatt drucken'), + $this->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment_id, 'print_files' => 1, 'user_ids[]' => $solver_id]), + Icon::create('print'), + ['target' => '_blank'] + ); + } + if ($assignment->isResetAllowed()) { + $widget->addLink( + _('Lösungen dieses Blatts löschen'), + $this->url_for('vips/sheets/delete_solutions', ['assignment_id' => $assignment_id, 'solver_id' => $solver_id]), + Icon::create('refresh'), + ['data-confirm' => _('Wollen Sie die Lösungen dieses Aufgabenblatts wirklich löschen?')] + )->asButton(); + } + Sidebar::get()->addWidget($widget); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Aufgabenblatt bearbeiten'), + $this->url_for('vips/sheets/edit_assignment', ['assignment_id' => $assignment_id]) + ); + $widget->addLink( + _('Studierendensicht (Vorschau)'), + $this->url_for('vips/sheets/show_assignment', ['assignment_id' => $assignment_id]) + )->setActive(); + Sidebar::get()->addWidget($widget); + + if ($assignment->range_type === 'course') { + $widget = new SelectWidget(_('Anzeigen für'), $this->url_for('vips/sheets/show_assignment', compact('assignment_id')), 'solver_id'); + $widget->class = 'nested-select'; + $element = new SelectElement($GLOBALS['user']->id, ' ', $GLOBALS['user']->id == $solver_id); + $widget->addElement($element); + + foreach ($assignment->course->members->findBy('status', 'autor')->orderBy('nachname, vorname') as $member) { + if ($assignment->isVisible($member->user_id)) { + $element = new SelectElement($member->user_id, $member->nachname . ', ' . $member->vorname, $member->user_id == $solver_id); + $widget->addElement($element); + } + } + Sidebar::get()->addWidget($widget); + } + } + } + + ##################################### + # # + # Lecturer Methods # + # # + ##################################### + + + /** + * Dialog for confirming the end date of a starting assignment. + */ + public function start_assignment_dialog_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + $this->assignment = $assignment; + } + + /** + * EXAMS/SHEETS + * + * If an assignment hasn't started yet this function sets the start time to NOW + * so that it's running + * + */ + public function start_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + $end_date = trim(Request::get('end_date')); + $end_time = trim(Request::get('end_time')); + $end_datetime = DateTime::createFromFormat('d.m.Y H:i', $end_date.' '.$end_time); + + // unlimited selftest + if ($assignment->type === 'selftest' && $end_date === '' && $end_time === '') { + $end = null; + } else if ($end_datetime) { + $end = strtotime($end_datetime->format('Y-m-d H:i:s')); + } else { + $end = $assignment->end; + PageLayout::postWarning(_('Ungültiger Endzeitpunkt, der Wert wurde nicht übernommen.')); + } + + // set new start and end time in database + $assignment->start = time(); + $assignment->end = $end; + $assignment->active = 1; + $assignment->store(); + + // delete start time for exam from database + VipsAssignmentAttempt::deleteBySQL('assignment_id = ?', [$assignment_id]); + + $this->redirect('vips/sheets'); + } + + + /** + * EXAMS/SHEETS + * + * Stops/continues an assignment (no change of start/end time but temporary closure) + * + */ + public function stopgo_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $db = DBManager::get(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + if ($assignment->type === 'exam') { + if ($assignment->active) { + $assignment->options['stopdate'] = date('Y-m-d H:i:s'); + } else if ($assignment->options['stopdate']) { + // extend exam duration for already active participants + $interval = time() - strtotime($assignment->options['stopdate']); + $sql = 'UPDATE etask_assignment_attempts SET end = end + ? + WHERE assignment_id = ? AND end > ?'; + $stmt = $db->prepare($sql); + $stmt->execute([$interval, $assignment_id, $assignment->options['stopdate']]); + + unset($assignment->options['stopdate']); + } + } + + $assignment->active = !$assignment->active; + $assignment->store(); + + $this->redirect('vips/sheets'); + } + + + /** + * EXAMS/SHEETS + * + * Deletes an assignment from the course (and block if applicable). + */ + public function delete_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $test_title = $assignment->test->title; + + VipsModule::requireEditPermission($assignment); + + if (!$assignment->isLocked()) { + $assignment->delete(); + PageLayout::postSuccess(sprintf(_('Das Aufgabenblatt „%s“ wurde gelöscht.'), htmlReady($test_title))); + } + + $this->redirect('vips/sheets'); + } + + /** + * Delete a list of assignments from the course (and block if applicable). + */ + public function delete_assignments_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_ids = Request::intArray('assignment_ids'); + $deleted = 0; + + foreach ($assignment_ids as $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + if (!$assignment->isLocked()) { + $assignment->delete(); + ++$deleted; + } + } + + if ($deleted > 0) { + PageLayout::postSuccess(sprintf(_('Es wurden %s Aufgabenblätter gelöscht.'), $deleted)); + } + + if ($deleted < count($assignment_ids)) { + PageLayout::postError(_('Einige Aufgabenblätter konnten nicht gelöscht werden, da bereits Lösungen abgegeben wurden.'), [ + _('Falls Sie diese wirklich löschen möchten, müssen Sie zuerst die Lösungen aller Teilnehmenden zurücksetzen.') + ]); + } + + $this->redirect(Context::getId() ? 'vips/sheets' : 'vips/pool/assignments'); + } + + /** + * Dialog for selecting a block for a list of assignments. + */ + public function assign_block_dialog_action() + { + $course_id = Context::getId(); + VipsModule::requireStatus('tutor', $course_id); + + $this->assignment_ids = Request::intArray('assignment_ids'); + $this->blocks = VipsBlock::findBySQL('range_id = ? ORDER BY name', [$course_id]); + } + + /** + * Assign a list of assignments to the specified block. + */ + public function assign_block_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_ids = Request::intArray('assignment_ids'); + $block_id = Request::int('block_id'); + + if ($block_id) { + $block = VipsBlock::find($block_id); + } + + foreach ($assignment_ids as $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + if (!$block_id || $block->range_id === $assignment->range_id) { + $assignment->block_id = $block_id ?: null; + $assignment->store(); + } + } + + PageLayout::postSuccess(_('Die Blockzuordnung wurde gespeichert.')); + + $this->redirect('vips/sheets'); + } + + /** + * Dialog for copying a list of assignments into a course. + */ + public function copy_assignments_dialog_action() + { + PageLayout::setTitle(_('Aufgabenblätter kopieren')); + + $this->assignment_ids = Request::intArray('assignment_ids'); + $this->courses = VipsModule::getActiveCourses($GLOBALS['user']->id); + $this->course_id = Context::getId(); + } + + /** + * Copy the selected assignments into the selected course. + */ + public function copy_assignments_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_ids = Request::intArray('assignment_ids'); + $course_id = Request::option('course_id'); + + if ($course_id) { + VipsModule::requireStatus('tutor', $course_id); + } + + foreach ($assignment_ids as $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + if ($course_id) { + $assignment->copyIntoCourse($course_id); + } else { + $assignment->copyIntoCourse($GLOBALS['user']->id, 'user'); + } + } + + PageLayout::postSuccess(ngettext('Das Aufgabenblatt wurde kopiert.', 'Die Aufgabenblätter wurden kopiert.', count($assignment_ids))); + + $this->redirect(Context::getId() ? 'vips/sheets' : 'vips/pool/assignments'); + } + + /** + * Dialog for moving a list of assignments to another course. + */ + public function move_assignments_dialog_action() + { + PageLayout::setTitle(_('Aufgabenblätter verschieben')); + + $this->assignment_ids = Request::intArray('assignment_ids'); + $this->courses = VipsModule::getActiveCourses($GLOBALS['user']->id); + $this->course_id = Context::getId(); + } + + /** + * Move a list of assignments to the specified course. + */ + public function move_assignments_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_ids = Request::intArray('assignment_ids'); + $course_id = Request::option('course_id'); + + if ($course_id) { + VipsModule::requireStatus('tutor', $course_id); + } + + foreach ($assignment_ids as $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + if ($course_id) { + $assignment->moveIntoCourse($course_id); + } else { + $assignment->moveIntoCourse($GLOBALS['user']->id, 'user'); + } + } + + PageLayout::postSuccess(ngettext('Das Aufgabenblatt wurde verschoben.', 'Die Aufgabenblätter wurden verschoben.', count($assignment_ids))); + + $this->redirect(Context::getId() ? 'vips/sheets' : 'vips/pool/assignments'); + } + + /** + * Delete the solutions of all students and reset the assignment. + */ + public function reset_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + if ($assignment->type === 'exam') { + $assignment->deleteAllSolutions(); + PageLayout::postSuccess(_('Die Klausur wurde zurückgesetzt und alle abgegebenen Lösungen archiviert.')); + } + + $this->redirect(Context::getId() ? 'vips/sheets' : 'vips/pool/assignments'); + } + + + /** + * SHEETS/EXAMS + * + * Takes an exercise off an assignment and deletes it. + */ + public function delete_exercise_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_id = Request::int('exercise_id'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $exercise = Exercise::find($exercise_id); + + VipsModule::requireEditPermission($assignment, $exercise_id); + + if (!$assignment->isLocked()) { + $assignment->test->removeExercise($exercise_id); + PageLayout::postSuccess(sprintf(_('Die Aufgabe „%s“ wurde gelöscht.'), htmlReady($exercise->title))); + } + + $this->redirect($this->url_for('vips/sheets/edit_assignment', compact('assignment_id'))); + } + + /** + * Deletes a list of exercises from a specific assignment. + */ + public function delete_exercises_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_ids = Request::intArray('exercise_ids'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + if (!$assignment->isLocked()) { + foreach ($exercise_ids as $exercise_id) { + VipsModule::requireEditPermission($assignment, $exercise_id); + $assignment->test->removeExercise($exercise_id); + } + + PageLayout::postSuccess(sprintf(_('Es wurden %s Aufgaben gelöscht.'), count($exercise_ids))); + } + + $this->redirect($this->url_for('vips/sheets/edit_assignment', compact('assignment_id'))); + } + + /** + * Reorder exercise positions within an assignment. + */ + public function move_exercise_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $list = Request::intArray('item'); + + VipsModule::requireEditPermission($assignment); + + /* renumber all exercises in current assignment */ + foreach ($list as $i => $exercise_id) { + $exercise_ref = VipsExerciseRef::find([$assignment->test_id, $exercise_id]); + + if ($exercise_ref) { + $exercise_ref->position = $i + 1; + $exercise_ref->store(); + } + } + + $this->render_nothing(); + } + + /** + * SHEETS/EXAMS + * + * Displays the form for editing an exercise. + * + * Is called when editing an existing exercise or creating a new exercise. + */ + public function edit_exercise_action() + { + PageLayout::setHelpKeyword('Basis.VipsAufgaben'); + + $exercise_id = Request::int('exercise_id'); // is not set when creating new exercise + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment, $exercise_id); + + if ($exercise_id) { + // edit already existing exercise + $exercise_ref = $assignment->test->getExerciseRef($exercise_id); + $exercise = $exercise_ref->exercise; + + $max_points = $exercise_ref->points; + $exercise_position = $exercise_ref->position; + } else { + // create new exercise + $exercise_type = Request::option('exercise_type'); + $exercise = new $exercise_type(); + + $max_points = null; + $exercise_position = null; + } + + $this->assignment = $assignment; + $this->assignment_id = $assignment_id; + $this->exercise = $exercise; + $this->exercise_position = $exercise_position; + $this->max_points = $max_points; + + $this->contentbar = $this->create_contentbar($assignment, $exercise_id); + + Helpbar::get()->addPlainText('', + _('Sie können hier den Aufgabentext und die Antwortoptionen dieser Aufgabe bearbeiten.')); + + $widget = new ActionsWidget(); + + if (!$assignment->isLocked()) { + $widget->addLink( + _('Neue Aufgabe erstellen'), + $this->url_for('vips/sheets/add_exercise_dialog', ['assignment_id' => $assignment_id]), + Icon::create('add') + )->asDialog('size=auto'); + } + + Sidebar::get()->addWidget($widget); + + if ($exercise->id) { + $widget = new ViewsWidget(); + $widget->addLink( + _('Aufgabe bearbeiten'), + $this->url_for('vips/sheets/edit_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $exercise->id]) + )->setActive(); + $widget->addLink( + _('Studierendensicht (Vorschau)'), + $this->url_for('vips/sheets/show_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $exercise->id]) + ); + Sidebar::get()->addWidget($widget); + } + + $widget = new ViewsWidget(); + $widget->setTitle(_('Aufgabenblatt')); + + foreach ($assignment->test->exercise_refs as $item) { + $widget->addLink( + sprintf(_('Aufgabe %d'), $item->position), + $this->url_for('vips/sheets/edit_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $item->task_id]) + )->setActive($item->task_id === $exercise->id); + } + + Sidebar::get()->addWidget($widget); + } + + + /** + * SHEETS/EXAMS + * + * Inserts/Updates an exercise into the database + */ + public function store_exercise_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $exercise_id = Request::int('exercise_id'); // not set when storing new exercise + $exercise_type = Request::option('exercise_type'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $test_id = $assignment->test_id; + $file_ids = Request::optionArray('file_ids'); + $request = Request::getInstance(); + + VipsModule::requireEditPermission($assignment, $exercise_id); + + if ($exercise_id) { + // update existing exercise. + $exercise = Exercise::find($exercise_id); + $item_count = $exercise->itemCount(); + $exercise->initFromRequest($request); + $exercise->store(); + + // update maximum points + if ($exercise->itemCount() != $item_count) { + $exercise_ref = VipsExerciseRef::find([$test_id, $exercise_id]); + $exercise_ref->points = $exercise->itemCount(); + $exercise_ref->store(); + } + } else { + // store exercise in database. + $exercise = new $exercise_type(); + $exercise->initFromRequest($request); + $exercise->user_id = $GLOBALS['user']->id; + $exercise->store(); + + // link new exercise to the assignment. + $assignment->test->addExercise($exercise); + $exercise_id = $exercise->id; + } + + $upload = $_FILES['upload'] ?: ['name' => []]; + $folder = Folder::findTopFolder($exercise->id, 'ExerciseFolder', 'task'); + + foreach ($folder->file_refs as $file_ref) { + if (!in_array($file_ref->id, $file_ids) || in_array($file_ref->name, $upload['name'])) { + $file_ref->delete(); + } + } + + FileManager::handleFileUpload($upload, $folder->getTypedFolder()); + + PageLayout::postSuccess(_('Die Aufgabe wurde eingetragen.')); + + $this->redirect($this->url_for('vips/sheets/edit_exercise', compact('assignment_id', 'exercise_id'))); + } + + /** + * Copy the selected exercises into this assignment. + */ + public function copy_exercise_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $exercise_id = Request::int('exercise_id'); + $exercise_ids = $exercise_id ? [$exercise_id => $assignment_id] : Request::intArray('exercise_ids'); + + VipsModule::requireEditPermission($assignment); + + if (!$assignment->isLocked()) { + foreach ($exercise_ids as $exercise_id => $copy_assignment_id) { + $copy_assignment = VipsAssignment::find($copy_assignment_id); + VipsModule::requireEditPermission($copy_assignment); + + $exercise_ref = VipsExerciseRef::find([$copy_assignment->test_id, $exercise_id]); + $exercise_ref->copyIntoTest($assignment->test_id); + } + + PageLayout::postSuccess(ngettext('Die Aufgabe wurde kopiert.', 'Die Aufgaben wurden kopiert.', count($exercise_ids))); + } + + $this->redirect($this->url_for('vips/sheets/edit_assignment', compact('assignment_id'))); + } + + /** + * Dialog for copying a list of exercises to another assignment. + */ + public function copy_exercises_dialog_action() + { + $this->assignment_id = Request::int('assignment_id'); + $this->exercise_ids = Request::intArray('exercise_ids'); + $this->courses = VipsModule::getActiveCourses($GLOBALS['user']->id); + } + + /** + * Copy a list of exercises to the specified assignment. + */ + public function copy_exercises_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $target_assignment_id = Request::int('target_assignment_id'); + $exercise_ids = Request::intArray('exercise_ids'); + + $assignment = VipsAssignment::find($assignment_id); + $target_assignment = VipsAssignment::find($target_assignment_id); + + VipsModule::requireEditPermission($assignment); + VipsModule::requireEditPermission($target_assignment); + + if (!$target_assignment->isLocked()) { + foreach ($exercise_ids as $exercise_id) { + $exercise_ref = $assignment->test->getExerciseRef($exercise_id); + $exercise_ref->copyIntoTest($target_assignment->test_id); + } + + PageLayout::postSuccess(ngettext('Die Aufgabe wurde kopiert.', 'Die Aufgaben wurden kopiert.', count($exercise_ids))); + } + + $this->redirect($this->url_for('vips/sheets/edit_assignment', compact('assignment_id'))); + } + + /** + * Dialog for moving a list of exercises to another assignment. + */ + public function move_exercises_dialog_action() + { + $this->assignment_id = Request::int('assignment_id'); + $this->exercise_ids = Request::intArray('exercise_ids'); + $this->courses = VipsModule::getActiveCourses($GLOBALS['user']->id); + } + + /** + * Move a list of exercises to the specified assignment. + */ + public function move_exercises_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $target_assignment_id = Request::int('target_assignment_id'); + $exercise_ids = Request::intArray('exercise_ids'); + + $assignment = VipsAssignment::find($assignment_id); + $target_assignment = VipsAssignment::find($target_assignment_id); + + VipsModule::requireEditPermission($assignment); + VipsModule::requireEditPermission($target_assignment); + + if (!$assignment->isLocked() && !$target_assignment->isLocked()) { + foreach ($exercise_ids as $exercise_id) { + $exercise_ref = VipsExerciseRef::find([$assignment->test_id, $exercise_id]); + $exercise_ref->moveIntoTest($target_assignment->test_id); + } + + PageLayout::postSuccess(ngettext('Die Aufgabe wurde verschoben.', 'Die Aufgaben wurden verschoben.', count($exercise_ids))); + } + + $this->redirect($this->url_for('vips/sheets/edit_assignment', compact('assignment_id'))); + } + + /** + * SHEETS/EXAMS + * + * Stores the specification (Grunddaten) of an assignment + * OR add new exercise, edit points/Bewertung (basically everything that can be done on + * page edit_exercise_action()) + */ + public function store_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $db = DBManager::get(); + + $assignment_id = Request::int('assignment_id'); + + if ($assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + } else { + $assignment = new VipsAssignment(); + $assignment->range_id = Context::getId() ?: $GLOBALS['user']->id; + $assignment->range_type = Context::getId() ? 'course' : 'user'; + } + + VipsModule::requireEditPermission($assignment); + + $assignment_name = trim(Request::get('assignment_name')); + $assignment_description = trim(Request::get('assignment_description')); + $assignment_description = Studip\Markup::purifyHtml($assignment_description); + $assignment_notes = trim(Request::get('assignment_notes')); + $assignment_type = Request::option('assignment_type', 'practice'); + $assignment_block = Request::int('assignment_block', 0); + $assignment_block_name = trim(Request::get('assignment_block_name')); + $start_date = trim(Request::get('start_date')); + $start_time = trim(Request::get('start_time')); + $end_date = trim(Request::get('end_date')); + $end_time = trim(Request::get('end_time')); + + $exam_length = Request::int('exam_length'); + $access_code = trim(Request::get('access_code')); + $ip_range = trim(Request::get('ip_range')); + $use_groups = Request::int('use_groups', 0); + $shuffle_answers = Request::int('shuffle_answers', 0); + $shuffle_exercises = Request::int('shuffle_exercises', 0); + $self_assessment = Request::int('self_assessment', 0); + $max_tries = Request::int('max_tries', 0); + $resets = Request::int('resets', 0); + $evaluation_mode = Request::int('evaluation_mode', 0); + $exercise_points = Request::floatArray('exercise_points'); + $selftest_threshold = Request::getArray('threshold'); + $selftest_feedback = Request::getArray('feedback'); + + $start_datetime = DateTime::createFromFormat('d.m.Y H:i', $start_date.' '.$start_time); + $end_datetime = DateTime::createFromFormat('d.m.Y H:i', $end_date.' '.$end_time); + + if ($assignment_name === '') { + $assignment_name = _('Aufgabenblatt'); + } + + if ($start_datetime) { + $start = $start_datetime->format('Y-m-d H:i:s'); + } else { + $start = date('Y-m-d H:00:00'); + PageLayout::postWarning(_('Ungültiger Startzeitpunkt, der Wert wurde nicht übernommen.')); + } + + // unlimited selftest + if ($assignment_type == 'selftest' && $end_date == '' && $end_time == '') { + $end = null; + } else if ($end_datetime) { + $end = $end_datetime->format('Y-m-d H:i:s'); + } else { + $end = date('Y-m-d H:00:00'); + PageLayout::postWarning(_('Ungültiger Endzeitpunkt, der Wert wurde nicht übernommen.')); + } + + if ($end && $end <= $start) { // start is *later* than end! + $end = $start; + PageLayout::postWarning(_('Bitte überprüfen Sie den Start- und den Endzeitpunkt!')); + } + + if ($assignment_block_name != '') { + $block = VipsBlock::create(['name' => $assignment_block_name, 'range_id' => $assignment->range_id]); + } else if ($assignment_block) { + $block = VipsBlock::find($assignment_block); + + if ($block->range_id !== $assignment->range_id) { + $block = null; + } + } else { + $block = null; + } + + foreach ($selftest_threshold as $i => $threshold) { + if ($threshold !== '') { + $feedback[$threshold] = Studip\Markup::purifyHtml($selftest_feedback[$i]); + } + } + + /*** store basic data (Grunddaten) of assignment */ + if ($assignment_id) { + // check whether the exam's start time has been moved + if ($assignment->start != strtotime($start) && time() <= strtotime($start)) { + $assignment->active = 1; + } + + // extend exam duration for already active participants + if ($assignment_type === 'exam' && $assignment->options['duration'] != $exam_length) { + $sql = 'UPDATE etask_assignment_attempts SET end = GREATEST(end + ? * 60, UNIX_TIMESTAMP()) + WHERE assignment_id = ? AND end > UNIX_TIMESTAMP()'; + $stmt = $db->prepare($sql); + $stmt->execute([$exam_length - $assignment->options['duration'], $assignment_id]); + } + + $assignment->test->setData([ + 'title' => $assignment_name, + 'description' => $assignment_description + ]); + $assignment->test->store(); + } else { + $assignment->test = VipsTest::create([ + 'title' => $assignment_name, + 'description' => $assignment_description, + 'user_id' => $GLOBALS['user']->id + ]); + } + + $assignment->setData([ + 'type' => $assignment_type, + 'start' => strtotime($start), + 'end' => $end ? strtotime($end) : null, + 'block_id' => $block ? $block->id : null + ]); + + // update options array + $assignment->options['evaluation_mode'] = $evaluation_mode; + $assignment->options['notes'] = $assignment_notes; + + unset($assignment->options['access_code']); + unset($assignment->options['ip_range']); + unset($assignment->options['shuffle_answers']); + unset($assignment->options['shuffle_exercises']); + unset($assignment->options['self_assessment']); + unset($assignment->options['use_groups']); + unset($assignment->options['max_tries']); + unset($assignment->options['resets']); + unset($assignment->options['feedback']); + + if ($assignment_type === 'exam') { + $assignment->options['duration'] = $exam_length; + + if ($access_code !== '') { + $assignment->options['access_code'] = $access_code; + } + + if ($ip_range !== '') { + $assignment->options['ip_range'] = $ip_range; + } + + $assignment->options['shuffle_answers'] = $shuffle_answers; + + if ($shuffle_exercises === 1) { + $assignment->options['shuffle_exercises'] = $shuffle_exercises; + } + + if ($self_assessment === 1) { + $assignment->options['self_assessment'] = $self_assessment; + } + } + + if ($assignment_type === 'practice') { + $assignment->options['use_groups'] = $use_groups; + } + + if ($assignment_type === 'selftest') { + $assignment->options['max_tries'] = $max_tries; + + if ($resets === 0) { + $assignment->options['resets'] = $resets; + } + + if (isset($feedback)) { + krsort($feedback); + $assignment->options['feedback'] = $feedback; + } + } + + $assignment->store(); + $assignment_id = $assignment->id; + + foreach ($assignment->test->exercise_refs as $exercise_ref) { + $points = $exercise_points[$exercise_ref->task_id]; + $exercise_ref->points = round($points * 2) / 2; + $exercise_ref->store(); + } + + PageLayout::postSuccess(_('Das Aufgabenblatt wurde gespeichert.')); + $this->redirect($this->url_for('vips/sheets/edit_assignment', compact('assignment_id'))); + } + + /** + * Returns the dialog content to create a new exercise. + */ + public function add_exercise_dialog_action() + { + PageLayout::setHelpKeyword('Basis.VipsAufgaben'); + + $assignment_id = Request::int('assignment_id'); + + $this->assignment_id = $assignment_id; + $this->exercise_types = Exercise::getExerciseTypes(); + } + + /** + * Returns the dialog content to copy an existing exercise. + */ + public function copy_exercise_dialog_action() + { + $assignment_id = Request::int('assignment_id'); + $search_filter = Request::getArray('search_filter'); + + $sort = Request::option('sort', 'start_time'); + $desc = Request::int('desc', $sort === 'start_time'); + $page = Request::int('page', 1); + $size = 15; + + if (empty($search_filter) || Request::submitted('reset_search')) { + $search_filter = array_fill_keys(['search_string', 'exercise_type'], ''); + $search_filter['range_type'] = Context::getId() ? 'course' : 'user'; + } + + if ($search_filter['range_type'] === 'course') { + $course_ids = array_column(VipsModule::getActiveCourses($GLOBALS['user']->id), 'id'); + } else { + $course_ids = [$GLOBALS['user']->id]; + } + + $exercises = $this->getAllExercises($course_ids, $sort, $desc, $search_filter); + + $this->sort = $sort; + $this->desc = $desc; + $this->page = $page; + $this->size = $size; + $this->count = count($exercises); + $this->exercises = array_slice($exercises, $size * ($page - 1), $size); + $this->exercise_types = Exercise::getExerciseTypes(); + $this->assignment_id = $assignment_id; + $this->search_filter = $search_filter; + } + + /** + * Get all matching exercises from a list of courses in given order. + * If $search_filter is not empty, search filters are applied. + * + * @param course_ids list of courses to get exercises from + * @param sort sort exercise list by this property + * @param desc true if sort direction is descending + * @param search_filter the currently active search filter + * + * @return array with data of all matching exercises + */ + public function getAllExercises($course_ids, $sort, $desc, $search_filter) + { + $db = DBManager::get(); + + // check if some filters are active + $search_string = $search_filter['search_string']; + $exercise_type = $search_filter['exercise_type']; + + $sql = "SELECT etask_tasks.*, + etask_assignments.id AS assignment_id, + etask_assignments.range_id, + etask_assignments.range_type, + etask_tests.title AS test_title, + seminare.name AS course_name, + (SELECT MIN(beginn) FROM semester_data + JOIN semester_courses USING(semester_id) + WHERE course_id = Seminar_id) AS start_time + FROM etask_tasks + JOIN etask_test_tasks ON etask_tasks.id = etask_test_tasks.task_id + JOIN etask_tests ON etask_tests.id = etask_test_tasks.test_id + JOIN etask_assignments USING (test_id) + LEFT JOIN seminare ON etask_assignments.range_id = seminare.seminar_id + WHERE etask_assignments.range_id IN (:course_ids) + AND etask_assignments.type IN ('exam', 'practice', 'selftest') " . + ($search_string ? 'AND (etask_tasks.title LIKE :input OR + etask_tasks.description LIKE :input OR + etask_tests.title LIKE :input OR + seminare.name LIKE :input) ' : '') . + ($exercise_type ? 'AND etask_tasks.type = :exercise_type ' : '') . + "ORDER BY :sort :desc, start_time DESC, seminare.name, + etask_tests.mkdate DESC, etask_test_tasks.position"; + + $stmt = $db->prepare($sql); + $stmt->bindValue(':course_ids', $course_ids); + $stmt->bindValue(':input', '%' . $search_string . '%'); + $stmt->bindValue(':exercise_type', $exercise_type); + $stmt->bindValue(':sort', $sort, StudipPDO::PARAM_COLUMN); + $stmt->bindValue(':desc', $desc ? 'DESC' : 'ASC', StudipPDO::PARAM_COLUMN); + $stmt->execute(); + + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + + /** + * SHEETS/EXAMS + * + * Displays form to edit an existing assignment + * + */ + public function edit_assignment_action() + { + PageLayout::setHelpKeyword('Basis.VipsAufgabenblatt'); + + $assignment_id = Request::int('assignment_id'); + + if ($assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + $test = $assignment->test; + } else { + $test = new VipsTest(); + $test->title = _('Aufgabenblatt'); + + $assignment = new VipsAssignment(); + $assignment->range_id = Context::getId() ?: $GLOBALS['user']->id; + $assignment->range_type = Context::getId() ? 'course' : 'user'; + $assignment->type = 'practice'; + $assignment->start = strtotime(date('Y-m-d H:00:00')); + $assignment->end = strtotime(date('Y-m-d H:00:00')); + } + + VipsModule::requireEditPermission($assignment); + + if (!isset($assignment->options['feedback'])) { + $assignment->options['feedback'] = ['' => '']; + } + + $blocks = VipsBlock::findBySQL('range_id = ? ORDER BY name', [$assignment->range_id]); + + $this->assignment = $assignment; + $this->assignment_id = $assignment_id; + $this->test = $test; + $this->blocks = $blocks; + $this->locked = $assignment_id && $assignment->isLocked(); + $this->exercises = $test->exercises; + $this->assignment_types = VipsAssignment::getAssignmentTypes(); + $this->exam_rooms = Config::get()->VIPS_EXAM_ROOMS; + + $this->contentbar = $this->create_contentbar($assignment); + + Helpbar::get()->addPlainText('', + _('Sie können hier die Grunddaten des Aufgabenblatts verwalten und Aufgaben hinzufügen, bearbeiten oder löschen.') . ' ' . + _('Alle Daten können später geändert oder ergänzt werden.')); + + $widget = new ActionsWidget(); + + if ($assignment_id && !$this->locked) { + $widget->addLink( + _('Neue Aufgabe erstellen'), + $this->url_for('vips/sheets/add_exercise_dialog', compact('assignment_id')), + Icon::create('add') + )->asDialog('size=auto'); + $widget->addLink( + _('Vorhandene Aufgabe kopieren'), + $this->url_for('vips/sheets/copy_exercise_dialog', compact('assignment_id')), + Icon::create('copy') + )->asDialog('size=big'); + } + + if ($assignment_id) { + if ($assignment->range_type === 'course') { + $widget->addLink( + _('Aufgabenblatt korrigieren'), + $this->url_for('vips/solutions/assignment_solutions', ['assignment_id' => $assignment_id]), + Icon::create('accept') + ); + } + + $widget->addLink( + _('Aufgabenblatt drucken'), + $this->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment_id]), + Icon::create('print'), + ['target' => '_blank'] + ); + Sidebar::get()->addWidget($widget); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Aufgabenblatt bearbeiten'), + $this->url_for('vips/sheets/edit_assignment', ['assignment_id' => $assignment_id]) + )->setActive(); + $widget->addLink( + _('Studierendensicht (Vorschau)'), + $this->url_for('vips/sheets/show_assignment', ['assignment_id' => $assignment_id]) + ); + Sidebar::get()->addWidget($widget); + + $widget = new ExportWidget(); + $widget->addLink( + _('Aufgabenblatt exportieren'), + $this->url_for('vips/sheets/export_xml', ['assignment_id' => $assignment_id]), + Icon::create('export') + ); + } + + Sidebar::get()->addWidget($widget); + } + + /** + * Show preview of an existing exercise (using print view for now). + */ + public function preview_exercise_action() + { + $exercise_id = Request::int('exercise_id'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment, $exercise_id); + + // fetch exercise info + $exercise_ref = VipsExerciseRef::find([$assignment->test_id, $exercise_id]); + $exercise = $exercise_ref->exercise; + + $this->assignment = $assignment; + $this->exercise = $exercise; + $this->exercise_position = $exercise_ref->position; + $this->max_points = $exercise_ref->points; + $this->solution = new VipsSolution(); + $this->show_solution = false; + $this->print_correction = false; + $this->user_id = null; + + $this->render_template('vips/exercises/print_exercise'); + } + + /** + * Copy the selected assignments into the current course. + */ + public function copy_assignment_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $course_id = Context::getId(); + + if ($course_id) { + VipsModule::requireStatus('tutor', $course_id); + } + + $assignment_id = Request::int('assignment_id'); + $assignment_ids = $assignment_id ? [$assignment_id] : Request::intArray('assignment_ids'); + + foreach ($assignment_ids as $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + if ($course_id) { + $assignment->copyIntoCourse($course_id); + } else { + $assignment->copyIntoCourse($GLOBALS['user']->id, 'user'); + } + } + + PageLayout::postSuccess(ngettext('Das Aufgabenblatt wurde kopiert.', 'Die Aufgabenblätter wurden kopiert.', count($assignment_ids))); + + $this->redirect($course_id ? 'vips/sheets' : 'vips/pool/assignments'); + } + + /** + * Imports a test from a text file. + */ + public function import_test_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $course_id = Context::getId(); + + if ($course_id) { + VipsModule::requireStatus('tutor', $course_id); + } + + if ($_FILES['upload']['name'][0] == '') { + PageLayout::postError(_('Sie müssen eine Datei zum Importieren auswählen.')); + $this->redirect($course_id ? 'vips/sheets' : 'vips/pool/assignments'); + return; + } + + $num_assignments = 0; + $num_exercises = 0; + + for ($i = 0; $i < count($_FILES['upload']['name']); ++$i) { + if (!is_uploaded_file($_FILES['upload']['tmp_name'][$i])) { + $message = sprintf(_('Es trat ein Fehler beim Hochladen der Datei „%s“ auf.'), htmlReady($_FILES['upload']['name'][$i])); + PageLayout::postError($message); + continue; + } + + $text = file_get_contents($_FILES['upload']['tmp_name'][$i]); + + if (str_contains($text, 'id, $course_id); + } else { + // convert from windows-1252 if legacy text format + $text = mb_decode_numericentity(mb_convert_encoding($text, 'UTF-8', 'WINDOWS-1252'), [0x100, 0xffff, 0, 0xffff], 'UTF-8'); + $test_title = trim(basename($_FILES['upload']['name'][$i], '.txt')); + $assignment = VipsAssignment::importText($test_title, $text, $GLOBALS['user']->id, $course_id); + } + + $num_assignments += 1; + $num_exercises += count($assignment->test->exercise_refs); + } + + if ($num_assignments == 1) { + $message = sprintf(ngettext('Das Aufgabenblatt „%s“ mit %d Aufgabe wurde hinzugefügt.', + 'Das Aufgabenblatt „%s“ mit %d Aufgaben wurde hinzugefügt.', $num_exercises), + htmlReady($assignment->test->title), $num_exercises); + PageLayout::postSuccess($message); + } else if ($num_assignments > 1) { + $message = sprintf(_('%1$d Aufgabenblätter mit insgesamt %2$d Aufgaben wurden hinzugefügt.'), $num_assignments, $num_exercises); + PageLayout::postSuccess($message); + } + + $this->redirect($course_id ? 'vips/sheets' : 'vips/pool/assignments'); + } + + /** + * Creates html print view of a sheet/exam (new window) specified by id + */ + public function print_assignments_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireViewPermission($assignment); + + $user_ids = Request::optionArray('user_ids'); + $print_files = Request::int('print_files'); + $print_correction = Request::int('print_correction'); + $print_sample_solution = Request::int('print_sample_solution'); + $print_student_ids = false; + $assignment_data = []; + + if (!$assignment->checkEditPermission()) { + $user_ids = [$GLOBALS['user']->id]; + $released = $assignment->releaseStatus($user_ids[0]); + $print_correction = $released >= VipsAssignment::RELEASE_STATUS_CORRECTIONS; + $print_sample_solution = $released == VipsAssignment::RELEASE_STATUS_SAMPLE_SOLUTIONS; + + if ($assignment->type !== 'exam' && $assignment->checkAccess($user_ids[0])) { + $assignment->recordAssignmentAttempt($user_ids[0]); + } else if ($released < VipsAssignment::RELEASE_STATUS_CORRECTIONS) { + PageLayout::postError(_('Kein Zugriff möglich!')); + $this->redirect('vips/sheets/list_assignments_stud'); + return; + } + } + + if ($assignment->range_type === 'course') { + foreach ($assignment->course->getMembersWithStatus('dozent') as $member) { + $lecturers[] = $member->getUserFullname(); + } + + $sem_class = $assignment->course->getSemClass(); + $print_student_ids = !$sem_class['studygroup_mode']; + } + + if ($user_ids) { + foreach ($user_ids as $user_id) { + $group = $assignment->getUserGroup($user_id); + $students = $stud_ids = []; + + if ($group) { + $name = $group->name; + $members = $assignment->getGroupMembers($group); + + usort($members, function($a, $b) { + return strcoll($a->user->getFullName('no_title_rev'), $b->user->getFullName('no_title_rev')); + }); + + foreach ($members as $member) { + $students[] = $member->user->getFullName('no_title'); + $stud_ids[] = $member->user->matriculation_number ?: _('(keine Matrikelnummer)'); + } + } else { + $user = User::find($user_id); + $name = $user->getFullName('no_title_rev'); + $students[] = $user->getFullName('no_title'); + $stud_ids[] = $user->matriculation_number ?: _('(keine Matrikelnummer)'); + } + + $assignment_data[] = [ + 'user_id' => $user_id, + 'students' => $students, + 'stud_ids' => $stud_ids + ]; + } + } else { + $assignment_data[] = [ + 'user_id' => null + ]; + } + + if (count($user_ids) === 1) { + Config::get()->UNI_NAME_CLEAN = $name; + } + + PageLayout::setTitle($assignment->test->title); + $this->set_layout('vips/sheets/print_layout'); + + $this->assignment = $assignment; + $this->user_ids = $user_ids; + $this->lecturers = $lecturers; + $this->print_files = $print_files; + $this->print_correction = $print_correction; + $this->print_sample_solution = $print_sample_solution; + $this->print_student_ids = $print_student_ids; + $this->assignment_data = $assignment_data; + } + + /** + * SHEETS/EXAMS + * + * Main page of sheets/exams. + * Lists all the assignments (sheets or exams) in the course, grouped by "not yet started", + * "running" and "finished". + */ + public function list_assignments_action() + { + $course_id = Context::getId(); + VipsModule::requireStatus('tutor', $course_id); + + $sort = Request::option('sort', 'start'); + $desc = Request::int('desc'); + $group = isset($_SESSION['group_assignments']) ? $_SESSION['group_assignments'] : 0; + $group = Request::int('group', $group); + + $_SESSION['group_assignments'] = $group; + $running = false; + + ###################################### + # get assignments in this course # + ###################################### + + $assignments = VipsAssignment::findByRangeId($course_id); + $blocks = VipsBlock::findBySQL('range_id = ? ORDER BY name', [$course_id]); + $blocks[] = VipsBlock::build(['name' => _('Aufgabenblätter ohne Blockzuordnung')]); + + usort($assignments, function($a, $b) use ($sort) { + if ($sort === 'title') { + return strcoll($a->test->title, $b->test->title); + } else if ($sort === 'type') { + return strcmp($a->type, $b->type); + } else if ($sort === 'start') { + return strcmp($a->start, $b->start); + } else { + return strcmp($a->end ?: '~', $b->end ?: '~'); + } + }); + + if ($desc) { + $assignments = array_reverse($assignments); + } + + $plugin_manager = PluginManager::getInstance(); + $courseware = $plugin_manager->getPluginInfo('CoursewareModule'); + $courseware_active = $courseware && $plugin_manager->isPluginActivated($courseware['id'], $course_id); + + if ($group == 2 && $courseware_active) { + $elements = Courseware\StructuralElement::findBySQL('range_id = ?', [$course_id]); + $unassigned = array_column($assignments, 'id'); + + foreach ($elements as $element) { + $assigned = $this->courseware_assignments($element); + $unassigned = array_diff($unassigned, $assigned); + + $assignment_data[] = [ + 'title' => $element->title, + 'assignments' => array_filter($assignments, function($assignment) use ($assigned) { + return in_array($assignment->id, $assigned); + }) + ]; + } + + $assignment_data[] = [ + 'title' => _('Aufgabenblätter ohne Courseware-Einbindung'), + 'assignments' => array_filter($assignments, function($assignment) use ($unassigned) { + return in_array($assignment->id, $unassigned); + }) + ]; + } else if ($group == 1) { + foreach ($blocks as $block) { + $assignment_data[$block->id] = [ + 'title' => $block->name, + 'block' => $block, + 'assignments' => [] + ]; + } + + foreach ($assignments as $assignment) { + $assignment_data[$assignment->block_id]['assignments'][] = $assignment; + } + } else { + $group = 0; + $assignment_data = [ + [ + 'title' => _('Noch nicht gestartete Aufgabenblätter'), + 'assignments' => [] + ], [ + 'title' => _('Laufende Aufgabenblätter'), + 'assignments' => [] + ], [ + 'title' => _('Beendete Aufgabenblätter'), + 'assignments' => [] + ] + ]; + + foreach ($assignments as $assignment) { + if ($assignment->isFinished()) { + $assignment_data[2]['assignments'][] = $assignment; + } else if ($assignment->isRunning()) { + $assignment_data[1]['assignments'][] = $assignment; + } else { + $assignment_data[0]['assignments'][] = $assignment; + } + } + } + + foreach ($assignments as $assignment) { + if ($assignment->isRunning()) { + $running = true; + } + } + + $this->assignment_data = $assignment_data; + $this->num_assignments = count($assignments); + $this->sort = $sort; + $this->desc = $desc; + $this->group = $group; + $this->blocks = $blocks; + + Helpbar::get()->addPlainText('', + _('Hier können Übungen, Tests und Klausuren online vorbereitet und durchgeführt werden. Sie erhalten ' . + 'dabei auch eine Übersicht über die Lösungen bzw. Antworten der Studierenden.') . "\n\n" . + _('Auf dieser Seite können Sie Aufgabenblätter in Ihrem Kurs anlegen und verwalten.')); + + $widget = new ActionsWidget(); + $widget->addLink( + _('Aufgabenblatt erstellen'), + $this->url_for('vips/sheets/edit_assignment'), + Icon::create('add') + ); + $widget->addLink( + _('Aufgabenblatt kopieren'), + $this->url_for('vips/sheets/copy_assignment_dialog'), + Icon::create('copy') + )->asDialog('size=1200x800'); + $widget->addLink( + _('Aufgabenblatt importieren'), + $this->url_for('vips/sheets/import_assignment_dialog'), + Icon::create('import') + )->asDialog('size=auto'); + $widget->addLink( + _('Neuen Block erstellen'), + $this->url_for('vips/admin/edit_block'), + Icon::create('add') + )->asDialog('size=auto'); + Sidebar::get()->addWidget($widget); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Gruppiert nach Status'), + $this->url_for('vips/sheets', ['group' => 0]) + )->setActive($group == 0); + $widget->addLink( + _('Gruppiert nach Blöcken'), + $this->url_for('vips/sheets', ['group' => 1]) + )->setActive($group == 1); + + if ($courseware_active) { + $widget->addLink( + _('Verwendung in Courseware'), + $this->url_for('vips/sheets', ['group' => 2]) + )->setActive($group == 2); + } + + Sidebar::get()->addWidget($widget); + } + + /** + * Collect all assignment_ids used in the given Courseware element. + */ + private function courseware_assignments($element) + { + $result = []; + + foreach ($element->containers as $container) { + foreach ($container->blocks as $block) { + if ($block->block_type === 'test') { + $payload = json_decode($block->payload, true); + + if ($payload['assignment']) { + $result[] = $payload['assignment']; + } + } + } + } + + return $result; + } + + /** + * Returns the dialog content to import an assignment from text file. + */ + public function import_assignment_dialog_action() + { + } + + /** + * Returns the dialog content to copy available assignments. + */ + public function copy_assignment_dialog_action() + { + $search_filter = Request::getArray('search_filter'); + + $sort = Request::option('sort', 'start_time'); + $desc = Request::int('desc', $sort === 'start_time'); + $page = Request::int('page', 1); + $size = 15; + + if (empty($search_filter) || Request::submitted('reset_search')) { + $search_filter = array_fill_keys(['search_string', 'assignment_type'], ''); + $search_filter['range_type'] = Context::getId() ? 'course' : 'user'; + } + + if ($search_filter['range_type'] === 'course') { + $course_ids = array_column(VipsModule::getActiveCourses($GLOBALS['user']->id), 'id'); + } else { + $course_ids = [$GLOBALS['user']->id]; + } + + $assignments = $this->getAllAssignments($course_ids, $sort, $desc, $search_filter); + + $this->sort = $sort; + $this->desc = $desc; + $this->page = $page; + $this->size = $size; + $this->count = count($assignments); + $this->assignments = array_slice($assignments, $size * ($page - 1), $size); + $this->assignment_types = VipsAssignment::getAssignmentTypes(); + $this->search_filter = $search_filter; + } + + /** + * Get all matching assignments from a list of courses in given order. + * If $search_filter is not empty, search filters are applied. + * + * @param array $course_ids list of courses to get assignments from + * @param string $sort sort assignment list by this property + * @param bool $desc true if sort direction is descending + * @param array $search_filter the currently active search filter + * + * @return array with data of all matching assignments + */ + public function getAllAssignments(array $course_ids, string $sort, bool $desc, array $search_filter) + { + $db = DBManager::get(); + + // check if some filters are active + $search_string = $search_filter['search_string']; + $assignment_type = $search_filter['assignment_type']; + $types = $assignment_type ? [$assignment_type] : ['exam', 'practice', 'selftest']; + + $sql = "SELECT etask_assignments.*, + etask_tests.title AS test_title, + seminare.name AS course_name, + (SELECT MIN(beginn) FROM semester_data + JOIN semester_courses USING(semester_id) + WHERE course_id = Seminar_id) AS start_time + FROM etask_tests + JOIN etask_assignments ON etask_tests.id = etask_assignments.test_id + LEFT JOIN seminare ON etask_assignments.range_id = seminare.seminar_id + WHERE etask_assignments.range_id IN (:course_ids) + AND etask_assignments.type IN (:types) " . + ($search_string ? 'AND (etask_tests.title LIKE :input OR + etask_tests.description LIKE :input OR + seminare.name LIKE :input) ' : '') . + "ORDER BY :sort :desc, start_time DESC, seminare.name, + etask_tests.mkdate DESC"; + + $stmt = $db->prepare($sql); + $stmt->bindValue(':course_ids', $course_ids); + $stmt->bindValue(':input', '%' . $search_string . '%'); + $stmt->bindValue(':types', $types); + $stmt->bindValue(':sort', $sort, StudipPDO::PARAM_COLUMN); + $stmt->bindValue(':desc', $desc ? 'DESC' : 'ASC', StudipPDO::PARAM_COLUMN); + $stmt->execute(); + + return $stmt->fetchAll(PDO::FETCH_ASSOC); + } + + /** + * Exports all exercises in this assignment in Vips XML format. + */ + public function export_xml_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + $this->set_content_type('text/xml; charset=UTF-8'); + header('Content-Disposition: attachment; ' . encode_header_parameter('filename', $assignment->test->title.'.xml')); + + $this->render_text($assignment->exportXML()); + } + + public function relay_action($action) + { + $params = func_get_args(); + $params[0] = $this; + $exercise_id = Request::int('exercise_id'); + $exercise = Exercise::find($exercise_id); + $action = $action . '_action'; + + $this->exercise = $exercise; + + if (method_exists($exercise, $action)) { + call_user_func_array([$exercise, $action], $params); + } else { + throw new InvalidArgumentException(get_class($exercise) . '::' . $action); + } + } + + /** + * Create a ContentBar for this assignment (if no exercise is specified) + * or for the given exercise on the assignment. + */ + public function create_contentbar( + VipsAssignment $assignment, + ?int $exercise_id = null, + string $view = 'edit', + ?string $solver_id = null + ) { + $toc = new TOCItem($assignment->test->title); + $toc->setURL($this->url_for("vips/sheets/{$view}_assignment", ['assignment_id' => $assignment->id])); + $toc->setActive($exercise_id === null); + + if ($view === 'edit') { + $exercise_refs = $assignment->test->exercise_refs; + } else { + $exercise_refs = $assignment->getExerciseRefs($solver_id); + } + + foreach ($exercise_refs as $i => $item) { + $child = new TOCItem(sprintf('%d. %s', $i + 1, $item->exercise->title)); + $child->setURL($this->url_for( + "vips/sheets/{$view}_exercise", + ['assignment_id' => $assignment->id, 'exercise_id' => $item->task_id, 'solver_id' => $solver_id] + )); + + $child->setActive($item->task_id == $exercise_id); + $toc->children[] = $child; + } + + foreach ($toc->children as $i => $item) { + if ($item->isActive()) { + $icons = $this->get_template_factory()->open('vips/sheets/content_bar_icons'); + + if ($i > 0) { + $icons->prev_exercise_url = $toc->children[$i - 1]->getURL(); + } + + if ($i < count($toc->children) - 1) { + $icons->next_exercise_url = $toc->children[$i + 1]->getURL(); + } + } + } + + return Studip\VueApp::create('ContentBar')->withProps([ + 'isContentBar' => true, + 'toc' => $toc + ])->withComponent( + 'ContentBarBreadcrumbs' + )->withSlot( + 'breadcrumb-list', sprintf("", json_encode($toc)) + )->withSlot( + 'buttons-left', $icons ?? '' + ); + } + + /** + * Return the appropriate CSS class for sortable column (if any). + * + * @param boolean $sort sort by this column + * @param boolean $desc set sort direction + */ + public function sort_class(bool $sort, ?bool $desc): string + { + return $sort ? ($desc ? 'sortdesc' : 'sortasc') : ''; + } + + /** + * Render a generic page chooser selector. The first occurence of '%d' + * in the URL is replaced with the selected page number. + * + * @param string $url URL for one of the pages + * @param string $count total number of entries + * @param string $page current page to display + * @param string|null $dialog Optional dialog attribute content + * @param int|null $page_size page size (defaults to system default) + * @return mixed + */ + public function page_chooser(string $url, string $count, string $page, ?string $dialog = null, ?int $page_size = null) + { + $template = $GLOBALS['template_factory']->open('shared/pagechooser'); + $template->dialog = $dialog; + $template->num_postings = $count; + $template->page = $page; + $template->perPage = $page_size ?: Config::get()->ENTRIES_PER_PAGE; + $template->pagelink = str_replace('%%25d', '%d', str_replace('%', '%%', $url)); + + return $template->render(); + } +} diff --git a/app/controllers/vips/solutions.php b/app/controllers/vips/solutions.php new file mode 100644 index 0000000..ca46922 --- /dev/null +++ b/app/controllers/vips/solutions.php @@ -0,0 +1,2521 @@ +sort = $sort; + $this->desc = $desc; + $this->course_id = $course_id; + $this->user_id = $GLOBALS['user']->id; + $this->test_data = $this->get_assignments_data($course_id, $this->user_id, $sort, $desc); + $this->blocks = VipsBlock::findBySQL('range_id = ? ORDER BY name', [$course_id]); + $this->blocks[] = VipsBlock::build(['name' => _('Aufgabenblätter ohne Blockzuordnung')]); + + foreach ($this->test_data['assignments'] as $assignment) { + $this->block_assignments[$assignment['assignment']->block_id][] = $assignment; + } + + $this->use_weighting = false; + + foreach ($this->blocks as $block) { + if ($block->weight !== null) { + if ($block->weight) { + $this->use_weighting = true; + } + } else if (isset($this->block_assignments[$block->id])) { + foreach ($this->block_assignments[$block->id] as $ass) { + if ($ass['assignment']->weight) { + $this->use_weighting = true; + } + } + } + } + + $settings = CourseConfig::get($course_id); + + // display course results if grades are defined for this course + if (!VipsModule::hasStatus('tutor', $course_id) && $settings->VIPS_COURSE_GRADES) { + $assignments = VipsAssignment::findBySQL("range_id = ? AND type IN ('exam', 'practice')", [$course_id]); + $show_overview = true; + + // find unreleased or unfinished assignments + foreach ($assignments as $assignment) { + if (!$this->use_weighting || $assignment->weight || $assignment->block_id && $assignment->block->weight) { + if ( + $assignment->isVisible($this->user_id) + && $assignment->releaseStatus($this->user_id) == VipsAssignment::RELEASE_STATUS_NONE + ) { + $show_overview = false; + } + } + } + + // if all assignments are finished and released + if ($show_overview) { + $this->overview_data = $this->participants_overview_data($course_id, $this->user_id); + } + } + + if (VipsModule::hasStatus('tutor', $course_id)) { + Helpbar::get()->addPlainText('', + _('Hier finden Sie eine Übersicht über den Korrekturstatus Ihrer Aufgabenblätter und können Aufgaben korrigieren. ' . + 'Außerdem können Sie hier die Einstellungen für die Notenberechnung in Ihrem Kurs vornehmen.')); + + $widget = new ActionsWidget(); + $widget->addLink( + _('Notenverteilung festlegen'), + $this->url_for('vips/admin/edit_grades'), + Icon::create('graph') + )->asDialog('size=auto'); + Sidebar::get()->addWidget($widget); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Ergebnisse'), + $this->url_for('vips/solutions') + )->setActive(); + $widget->addLink( + _('Punkteübersicht'), + $this->url_for('vips/solutions/participants_overview', ['display' => 'points']) + ); + $widget->addLink( + _('Notenübersicht'), + $this->url_for('vips/solutions/participants_overview', ['display' => 'weighting']) + ); + $widget->addLink( + _('Statistik'), + $this->url_for('vips/solutions/statistics') + ); + Sidebar::get()->addWidget($widget); + } + } + + /** + * Changes which correction information is released to the student (either + * nothing or only the points or points and correction). + */ + public function update_released_dialog_action() + { + PageLayout::setTitle(_('Freigabe für Studierende')); + + $this->assignment_ids = Request::intArray('assignment_ids'); + + foreach ($this->assignment_ids as $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + $released = $assignment->options['released']; + $default = isset($default) ? ($released === $default ? $default : -1) : $released; + + if ($assignment->type === 'exam') { + $this->exam_options = true; + } + } + + $this->default = $default; + } + + /** + * Changes which correction information is released to the student (either + * nothing or only the points or points and correction). + */ + public function update_released_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_ids = Request::intArray('assignment_ids'); + $released = Request::int('released'); + + if (isset($released)) { + foreach ($assignment_ids as $assignment_id) { + $assignment = VipsAssignment::find($assignment_id); + VipsModule::requireEditPermission($assignment); + + $assignment->options['released'] = $released; + $assignment->store(); + } + + PageLayout::postSuccess(_('Die Freigabeeinstellungen wurden geändert.')); + } + + $this->redirect('vips/solutions'); + } + + /** + * Changes which correction information is released to the student (either + * nothing or only the points or points and correction). + */ + public function change_released_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + $assignment->options['released'] = Request::int('released'); + $assignment->store(); + + $this->redirect($this->url_for('vips/solutions/assignment_solutions', compact('assignment_id'))); + } + + /** + * Shows solution points for each student/group with a link to view solution and correct it. + */ + public function assignment_solutions_action() + { + PageLayout::setHelpKeyword('Basis.VipsKorrektur'); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $format = Request::option('format'); + + VipsModule::requireEditPermission($assignment); + + $view = Request::option('view'); + $expand = Request::option('expand'); + + // fetch info about assignment + $end = $assignment->end; + $duration = $assignment->options['duration']; + $released = $assignment->options['released']; + + // fetch solvers, exercises and solutions // + $arrays = $this->get_solutions($assignment, $view); + $solvers = $arrays['solvers']; + $exercises = $arrays['exercises']; + $solutions = $arrays['solutions']; + + if ($assignment->type === 'exam') { + $all_solvers = $solvers; + $solvers = []; + $started = []; + + // find all user ids which have an entry in etask_assignment_attempts + foreach ($assignment->assignment_attempts as $attempt) { + $start = $attempt->start; + $user_end = $attempt->end ? $attempt->end : $start + $duration * 60; + $user_end = min($end, $user_end); + $remaining_time = ceil(($user_end - time()) / 60); + + $started[$attempt->user_id] = [ + 'start' => $start, + 'remaining' => $remaining_time, + 'ip' => $attempt->ip_address + ]; + } + + // remove users which are not shown + foreach ($all_solvers as $solver) { + $user_id = $solver['id']; + + if (isset($started[$user_id])) { + $remaining = $started[$user_id]['remaining']; + + if ($view === 'working' && $remaining > 0 || $view == '' && $remaining <= 0) { + // working or finished + $solvers[$user_id] = $all_solvers[$user_id]; + $solvers[$user_id]['running_info'] = $started[$user_id]; + } + } else if ($view === 'pending') { + if ($assignment->isVisible($user_id)) { + // not yet started + $solvers[$user_id] = $all_solvers[$user_id]; + } + } + } + } + + /* reached points, uncorrected solutions and unanswered exercises */ + + $overall_uncorrected_solutions = 0; + $first_uncorrected_solution = null; + + foreach ($solvers as $solver_id => $solver) { + $extra_info = [ + 'points' => 0, + 'progress' => 0, + 'uncorrected' => 0, + 'unanswered' => count($exercises), + 'files' => 0 + ]; + + if (isset($solutions[$solver_id])) { + foreach ($solutions[$solver_id] as $solution) { + $extra_info['points'] += $solution['points']; + $extra_info['progress'] += $exercises[$solution['exercise_id']]['points']; + $extra_info['uncorrected'] += $solution['corrected'] ? 0 : 1; + $extra_info['unanswered'] -= 1; + $extra_info['files'] += $solution['uploads']; + + if (!$solution['corrected']) { + if (!isset($first_uncorrected_solution)) { + $first_uncorrected_solution = [ + 'solver_id' => $solver['user_id'], + 'exercise_id' => $solution['exercise_id'], + ]; + } + } + } + } + + $overall_uncorrected_solutions += $extra_info['uncorrected']; + $solvers[$solver_id]['extra_info'] = $extra_info; + } + + $this->assignment = $assignment; + $this->assignment_id = $assignment_id; + $this->view = $view; + $this->expand = $expand; + $this->solutions = $solutions; + $this->solvers = $solvers; + $this->exercises = $exercises; + $this->overall_max_points = $assignment->test->getTotalPoints(); + $this->overall_uncorrected_solutions = $overall_uncorrected_solutions; + $this->first_uncorrected_solution = $first_uncorrected_solution; + + if ($format === 'csv') { + $columns = [_('Teilnehmende')]; + + foreach ($exercises as $exercise) { + $columns[] = $exercise['position'] . '. ' . $exercise['title']; + } + + $columns[] = _('Summe'); + + $data = [$columns]; + + setlocale(LC_NUMERIC, $_SESSION['_language'] . '.UTF-8'); + + $row = [_('Maximalpunktzahl:')]; + + foreach ($exercises as $exercise) { + $row[] = sprintf('%g', $exercise['points']); + } + + $row[] = sprintf('%g', $this->overall_max_points); + + $data[] = $row; + + foreach ($solvers as $solver) { + $row = [$solver['name']]; + + foreach ($exercises as $exercise) { + if (isset($solutions[$solver['id']][$exercise['id']])) { + $row[] = sprintf('%g', $solutions[$solver['id']][$exercise['id']]['points']); + } else { + $row[] = ''; + } + } + + $row[] = sprintf('%g', $solver['extra_info']['points']); + + $data[] = $row; + } + + setlocale(LC_NUMERIC, 'C'); + + $this->render_csv($data, $assignment->test->title . '.csv'); + } else { + Helpbar::get()->addPlainText('', + _('In dieser Übersicht können Sie sich anzeigen lassen, welche Teilnehmenden Lösungen abgegeben haben, und diese Lösungen korrigieren und freigeben.')); + + $widget = new ActionsWidget(); + $widget->addLink( + _('Aufgabenblatt bearbeiten'), + $this->url_for('vips/sheets/edit_assignment', ['assignment_id' => $assignment_id]), + Icon::create('edit') + ); + $widget->addLink( + _('Aufgabenblatt drucken'), + $this->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment_id]), + Icon::create('print'), + ['target' => '_blank'] + ); + $widget->addLink( + _('Autokorrektur starten'), + $this->url_for('vips/solutions/autocorrect_dialog', compact('assignment_id', 'expand', 'view')), + Icon::create('accept') + )->asDialog('size=auto'); + + if ($assignment->type === 'exam') { + $widget->addLink( + _('Alle Lösungen zurücksetzen'), + $this->url_for('vips/solutions/delete_solutions', compact('assignment_id', 'view') + ['solver_id' => 'all']), + Icon::create('refresh'), + ['data-confirm' => _('Achtung: Wenn Sie die Lösungen zurücksetzen, werden die Lösungen aller Teilnehmenden archiviert!')] + )->asButton(); + } + + $plugin_manager = PluginManager::getInstance(); + $gradebook = $plugin_manager->getPluginInfo('GradebookModule'); + + if ($gradebook && $plugin_manager->isPluginActivated($gradebook['id'], $assignment->range_id)) { + if ($assignment->options['gradebook_id']) { + $widget->addLink( + _('Gradebook-Eintrag entfernen'), + $this->url_for('vips/solutions/gradebook_unpublish', compact('assignment_id', 'expand', 'view')), + Icon::create('assessment'), + ['data-confirm' => _('Eintrag aus dem Gradebook löschen?')] + )->asButton(); + } else { + $widget->addLink( + _('Eintrag im Gradebook anlegen'), + $this->url_for('vips/solutions/gradebook_dialog', compact('assignment_id', 'expand', 'view')), + Icon::create('assessment') + )->asDialog('size=auto'); + } + } + + Sidebar::get()->addWidget($widget); + + $widget = new ExportWidget(); + $widget->addLink( + _('Punktetabelle exportieren'), + $this->url_for('vips/solutions/assignment_solutions', ['assignment_id' => $assignment_id, 'format' => 'csv']), + Icon::create('export') + ); + + if ($assignment->type === 'exam') { + $widget->addLink( + _('Abgabeprotokolle exportieren'), + $this->url_for('vips/solutions/assignment_logs', ['assignment_id' => $assignment_id]), + Icon::create('export') + ); + } + + $widget->addLink( + _('Lösungen der Teilnehmenden exportieren'), + $this->url_for('vips/solutions/download_responses', ['assignment_id' => $assignment_id]), + Icon::create('export') + ); + $widget->addLink( + _('Abgegebene Dateien herunterladen'), + $this->url_for('vips/solutions/download_all_uploads', ['assignment_id' => $assignment_id]), + Icon::create('export') + ); + Sidebar::get()->addWidget($widget); + + $widget = new OptionsWidget(); + $widget->setTitle(_('Freigabe für Studierende')); + $widget->addRadioButton( + _('Nichts'), + $this->url_for('vips/solutions/change_released', [ + 'assignment_id' => $assignment_id, + 'released' => VipsAssignment::RELEASE_STATUS_NONE, + ]), + $released == VipsAssignment::RELEASE_STATUS_NONE + ); + $widget->addRadioButton( + _('Vergebene Punkte'), + $this->url_for('vips/solutions/change_released', [ + 'assignment_id' => $assignment_id, + 'released' => VipsAssignment::RELEASE_STATUS_POINTS, + ]), + $released == VipsAssignment::RELEASE_STATUS_POINTS + ); + $widget->addRadioButton( + _('Punkte und Kommentare'), + $this->url_for('vips/solutions/change_released', [ + 'assignment_id' => $assignment_id, + 'released' => VipsAssignment::RELEASE_STATUS_COMMENTS, + ]), + $released == VipsAssignment::RELEASE_STATUS_COMMENTS + ); + $widget->addRadioButton( + _('… zusätzlich Aufgaben und Korrektur'), + $this->url_for('vips/solutions/change_released', [ + 'assignment_id' => $assignment_id, + 'released' => VipsAssignment::RELEASE_STATUS_CORRECTIONS, + ]), + $released == VipsAssignment::RELEASE_STATUS_CORRECTIONS + ); + $widget->addRadioButton( + _('… zusätzlich Musterlösungen'), + $this->url_for('vips/solutions/change_released', [ + 'assignment_id' => $assignment_id, + 'released' => VipsAssignment::RELEASE_STATUS_SAMPLE_SOLUTIONS, + ]), + $released == VipsAssignment::RELEASE_STATUS_SAMPLE_SOLUTIONS + ); + Sidebar::get()->addWidget($widget); + } + } + + /** + * Download responses to all exercises for all users in an assignment. + */ + public function download_responses_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + $arrays = $this->get_solutions($assignment, null); + $columns = [_('Teilnehmende')]; + $exercises = []; + $item_count = []; + + foreach ($arrays['exercises'] as $exercise) { + $exercises[$exercise['id']] = Exercise::find($exercise['id']); + $item_count[$exercise['id']] = $exercises[$exercise['id']]->itemCount(); + + for ($i = 0; $i < $item_count[$exercise['id']]; ++$i) { + if ($i === 0) { + $columns[] = $exercise['position'] . '. ' . $exercise['title']; + } else { + $columns[] = ''; + } + } + + if ($exercises[$exercise['id']]->options['comment']) { + $columns[] = _('Bemerkungen'); + } + } + + $data = [$columns]; + + foreach ($arrays['solvers'] as $solver) { + $row = [$solver['name']]; + + if (isset($arrays['solutions'][$solver['id']])) { + $solutions = $arrays['solutions'][$solver['id']]; + + foreach ($arrays['exercises'] as $exercise) { + $vips_solution = null; + $export = []; + + if (isset($solutions[$exercise['id']])) { + $vips_solution = VipsSolution::find($solutions[$exercise['id']]['id']); + $export = $exercises[$exercise['id']]->exportResponse($vips_solution->response); + } + + for ($i = 0; $i < $item_count[$exercise['id']]; ++$i) { + $row[] = isset($export[$i]) ? $export[$i] : ''; + } + + if ($exercises[$exercise['id']]->options['comment']) { + $row[] = $vips_solution ? $vips_solution->student_comment : ''; + } + } + + $data[] = $row; + } + } + + $this->render_csv($data, $assignment->test->title . '.csv'); + } + + /** + * Download uploads to current solutions for all users in an assignment. + */ + public function download_all_uploads_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + $sem_class = $assignment->course->getSemClass(); + $filename = $assignment->test->title . '.zip'; + $zipfile = tempnam($GLOBALS['TMP_PATH'], 'upload'); + $zip = new ZipArchive(); + + if (!$zip->open($zipfile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { + throw new Exception(_('ZIP-Archiv konnte nicht erzeugt werden.')); + } + + $arrays = $this->get_solutions($assignment, null); + + foreach ($arrays['solvers'] as $solver) { + foreach ($arrays['exercises'] as $exercise) { + $solution = $arrays['solutions'][$solver['id']][$exercise['id']]; // may be null + $folder = $solver['name']; + + if ($solver['type'] === 'single' && !$sem_class['studygroup_mode']) { + $folder .= sprintf(' (%s)', $solver['stud_id'] ?: $solver['username']); + } + + if ($solution && $solution['uploads']) { + foreach (VipsSolution::find($solution['id'])->folder->file_refs as $file_ref) { + $zip->addFile($file_ref->file->getPath(), sprintf(_('%s/Aufgabe %d/'), $folder, $exercise['position']) . $file_ref->name); + } + } + } + } + + $zip->close(); + + if (!file_exists($zipfile)) { + file_put_contents($zipfile, base64_decode('UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==')); + } + + header('Content-Type: application/zip'); + header('Content-Disposition: attachment; ' . encode_header_parameter('filename', $filename)); + header('Content-Length: ' . filesize($zipfile)); + + readfile($zipfile); + unlink($zipfile); + die(); + } + + /** + * Download uploads to current solutions for a user in an assignment. + */ + public function download_uploads_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id'); + + VipsModule::requireEditPermission($assignment); + + $group = $assignment->getUserGroup($solver_id); + $solver_name = $group ? $group->name : get_username($solver_id); + + $filename = $assignment->test->title . '-' . $solver_name . '.zip'; + $zipfile = tempnam($GLOBALS['TMP_PATH'], 'upload'); + $zip = new ZipArchive(); + + if (!$zip->open($zipfile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { + throw new Exception(_('ZIP-Archiv konnte nicht erzeugt werden.')); + } + + foreach ($assignment->test->exercises as $i => $exercise) { + $solution = $assignment->getSolution($solver_id, $exercise->id); + + if ($solution) { + foreach ($solution->folder->file_refs as $file_ref) { + $zip->addFile($file_ref->file->getPath(), sprintf(_('Aufgabe %d/'), $i + 1) . $file_ref->name); + } + } + } + + $zip->close(); + + header('Content-Type: application/zip'); + header('Content-Disposition: attachment; ' . encode_header_parameter('filename', $filename)); + header('Content-Length: ' . filesize($zipfile)); + + readfile($zipfile); + unlink($zipfile); + die(); + } + + /** + * Show dialog for publishing the assignment in the gradebook. + */ + public function gradebook_dialog_action() + { + $this->assignment_id = Request::int('assignment_id'); + $this->assignment = VipsAssignment::find($this->assignment_id); + $this->view = Request::option('view'); + $this->expand = Request::option('expand'); + + VipsModule::requireEditPermission($this->assignment); + + $definitions = Grading\Definition::findByCourse_id($this->assignment->range_id); + $this->weights = array_sum(array_column($definitions, 'weight')); + } + + /** + * Publish this assignment in the gradebook of the course. + */ + public function gradebook_publish_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $view = Request::option('view'); + $expand = Request::option('expand'); + $title = Request::get('title'); + $weight = Request::float('weight'); + + VipsModule::requireEditPermission($assignment); + + $assignment->insertIntoGradebook($title, $weight); + $assignment->updateGradebookEntries(); + + PageLayout::postSuccess(_('Das Aufgabenblatt wurde in das Gradebook eingetragen.')); + $this->redirect($this->url_for('vips/solutions/assignment_solutions', compact('assignment_id', 'view', 'expand'))); + } + + /** + * Remove this assignment from the gradebook of the course. + */ + public function gradebook_unpublish_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $view = Request::option('view'); + $expand = Request::option('expand'); + + VipsModule::requireEditPermission($assignment); + + $assignment->removeFromGradebook(); + + PageLayout::postSuccess(_('Das Aufgabenblatt wurde aus dem Gradebook gelöscht.')); + $this->redirect($this->url_for('vips/solutions/assignment_solutions', compact('assignment_id', 'view', 'expand'))); + } + + /** + * Download a summary of the event logs for an assignment. + */ + public function assignment_logs_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment); + + $columns = [_('Nachname'), _('Vorname'), _('Kennung'), _('Ereignis'), + _('Zeit'), _('IP-Adresse'), _('Rechnername'), _('Sitzungs-ID')]; + $data = []; + + foreach ($assignment->assignment_attempts as $assignment_attempt) { + foreach ($assignment_attempt->getLogEntries() as $entry) { + $data[] = [ + $assignment_attempt->user->nachname, + $assignment_attempt->user->vorname, + $assignment_attempt->user->username, + $entry['label'], + $entry['time'], + $entry['ip_address'], + $entry['ip_address'] ? $this->gethostbyaddr($entry['ip_address']) : '', + $entry['session_id'] + ]; + } + } + + usort($data, function($a, $b) { + return strcoll("{$a[0]},{$a[1]},{$a[2]},{$a[4]}", "{$b[0]},{$b[1]},{$b[2]},{$b[4]}"); + }); + + array_unshift($data, $columns); + + $this->render_csv($data, $assignment->test->title . '_log.csv'); + } + + + + /******************************************************************************/ + /* + /* A U T O K O R R E K T U R + /* + /******************************************************************************/ + + /** + * Select options and run automatic correction of solutions. + */ + public function autocorrect_dialog_action() + { + $this->assignment_id = Request::int('assignment_id'); + $this->view = Request::option('view'); + $this->expand = Request::option('expand'); + } + + /** + * Deletes all solution-points, where the solutions are automatically corrected + */ + public function autocorrect_solutions_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $view = Request::option('view'); + $expand = Request::option('expand'); + $corrected = Request::int('corrected', 0); + + VipsModule::requireEditPermission($assignment); + + $corrected_solutions = 0; + + // select all solutions not manually corrected + $solutions = $assignment->solutions->findBy('grader_id', null); + + foreach ($solutions as $solution) { + $assignment->correctSolution($solution, $corrected); + $solution->store(); + + if ($solution->state) { + ++$corrected_solutions; + } + } + + $message = sprintf(ngettext('Es wurde %d Lösung korrigiert.', 'Es wurden %d Lösungen korrigiert.', $corrected_solutions), $corrected_solutions); + PageLayout::postSuccess($message); + $this->redirect($this->url_for('vips/solutions/assignment_solutions', compact('assignment_id', 'view', 'expand'))); + } + + /** + * Display form that allows lecturer to correct the student's solution. + */ + public function edit_solution_action() + { + PageLayout::setHelpKeyword('Basis.VipsKorrektur'); + + $exercise_id = Request::int('exercise_id'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireEditPermission($assignment, $exercise_id); + + $archived_id = Request::int('solution_id'); + $solver_id = Request::option('solver_id'); + $view = Request::option('view'); + + $group = $assignment->getUserGroup($solver_id); + $solver_name = $group ? $group->name : get_fullname($solver_id, 'no_title_rev'); + $solver_or_group_id = $group ? $group->id : $solver_id; + + // fetch solvers, exercises and solutions // + + $arrays = $this->get_solutions($assignment, $view); + $solvers = $arrays['solvers']; + $exercises = $arrays['exercises']; + $solutions = $arrays['solutions']; + + // previous and next solver, exercise and uncorrected exercise // + + $prev_solver = null; + $prev_exercise = null; + $next_solver = null; + $next_exercise = null; + $next_uncorrected_exercise = null; + $before_current = true; // before current solver + current exercise + + foreach ($solvers as $solver) { + foreach ($exercises as $exercise) { + // current solver and current exercise + if ($solver['id'] == $solver_or_group_id && $exercise['id'] == $exercise_id) { + $before_current = false; + $exercise_position = $exercise['position']; + $max_points = $exercise['points']; + continue; + } + + if (isset($solutions[$solver['id']][$exercise['id']])) { + // previous/next solver (same exercise) + if ($solver['id'] != $solver_or_group_id && $exercise['id'] == $exercise_id) { + if ($before_current) { + $prev_solver = $solver; + } else if (!isset($next_solver)) { + $next_solver = $solver; + } + } + + // previous/next exercise (same solver) + if ($solver['id'] == $solver_or_group_id && $exercise['id'] != $exercise_id) { + if ($before_current) { + $prev_exercise = $exercise; + } else if (!isset($next_exercise)) { + $next_exercise = $exercise; + } + } + + // previous/next uncorrected exercise + if (!$solutions[$solver['id']][$exercise['id']]['corrected']) { + if ($before_current) { + $prev_uncorrected_exercise = [ + 'id' => $exercise['id'], + 'solver_id' => $solver['user_id'] + ]; + } else if (!isset($next_uncorrected_exercise)) { + $next_uncorrected_exercise = [ + 'id' => $exercise['id'], + 'solver_id' => $solver['user_id'] + ]; + } + } + + // break condition + if (isset($next_uncorrected_exercise) && isset($next_solver)) { + break 2; + } + } + } + } + + ################################### + # get user solution if applicable # + ################################### + + $exercise = Exercise::find($exercise_id); + $solution = $assignment->getSolution($solver_id, $exercise_id); + $solution_archive = $assignment->getArchivedSolutions($solver_id, $exercise_id); + + if (!$solution) { + $solution = new VipsSolution(); + $solution->assignment = $assignment; + $version = _('Nicht abgegeben'); + } else { + $version = date('d.m.Y, H:i', $solution->mkdate); + } + + if ($assignment->type !== 'selftest' && !isset($solution->feedback)) { + $solution->feedback = $exercise->options['feedback']; + } + + if ($archived_id) { + foreach ($solution_archive as $old_solution) { + if ($old_solution->id == $archived_id) { + $solution = $old_solution; + break; + } + } + } + + ############################## + # set template variables # + ############################## + + $this->exercise = $exercise; + $this->exercise_id = $exercise_id; + $this->exercise_position = $exercise_position; + $this->assignment = $assignment; + $this->assignment_id = $assignment_id; + $this->solver_id = $solver_id; + $this->solver_name = $solver_name; + $this->solver_or_group_id = $solver_or_group_id; + $this->solution = $solution; + $this->edit_solution = !$archived_id; + $this->show_solution = true; + $this->max_points = $max_points; + $this->prev_solver = $prev_solver; + $this->prev_exercise = $prev_exercise; + $this->next_solver = $next_solver; + $this->next_exercise = $next_exercise; + $this->view = $view; + + Helpbar::get()->addPlainText('', + _('Sie können hier die Ergebnisse der Autokorrektur ansehen und Aufgaben manuell korrigieren.')); + + $widget = new ActionsWidget(); + $widget->addLink( + _('Aufgabe bearbeiten'), + $this->url_for('vips/sheets/edit_exercise', compact('assignment_id', 'exercise_id')), + Icon::create('edit') + ); + $widget->addLink( + _('Aufgabenblatt drucken'), + $this->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment_id, 'user_ids[]' => $solver_id, 'print_files' => 1, 'print_correction' => !$view]), + Icon::create('print'), + ['target' => '_blank'] + ); + Sidebar::get()->addWidget($widget); + + $widget = new LinksWidget(); + $widget->setTitle(_('Links')); + if (isset($prev_uncorrected_exercise)) { + $widget->addLink( + _('Vorige unkorrigierte Aufgabe'), + $this->url_for('vips/solutions/edit_solution', [ + 'assignment_id' => $assignment_id, + 'exercise_id' => $prev_uncorrected_exercise['id'], + 'solver_id' => $prev_uncorrected_exercise['solver_id'], + 'view' => $view, + ]), + Icon::create('arr_1left') + ); + } + if (isset($next_uncorrected_exercise)) { + $widget->addLink( + _('Nächste unkorrigierte Aufgabe'), + $this->url_for('vips/solutions/edit_solution', [ + 'assignment_id' => $assignment_id, + 'exercise_id' => $next_uncorrected_exercise['id'], + 'solver_id' => $next_uncorrected_exercise['solver_id'], + 'view' => $view, + ]), + Icon::create('arr_1right') + ); + } + Sidebar::get()->addWidget($widget); + + $widget = new SelectWidget(_('Aufgabenblatt'), $this->url_for('vips/solutions/edit_solution', compact('assignment_id', 'solver_id', 'view')), 'exercise_id'); + + foreach ($exercises as $exercise) { + $element = new SelectElement($exercise['id'], sprintf(_('Aufgabe %d'), $exercise['position']), $exercise['id'] === $exercise_id); + $widget->addElement($element); + } + Sidebar::get()->addWidget($widget); + + $widget = new SelectWidget(_('Versionen'), $this->url_for('vips/solutions/edit_solution', compact('assignment_id', 'exercise_id', 'solver_id', 'view')), 'solution_id'); + $element = new SelectElement(0, sprintf(_('Aktuelle Version: %s'), $version), !$archived_id); + $widget->addElement($element); + + if (count($solution_archive) === 0) { + $widget->attributes = ['disabled' => 'disabled']; + } + + foreach ($solution_archive as $i => $old_solution) { + $element = new SelectElement($old_solution->id, + sprintf(_('Version %s vom %s'), count($solution_archive) - $i, date('d.m.Y, H:i', $old_solution->mkdate)), + $old_solution->id == $archived_id); + $widget->addElement($element); + } + Sidebar::get()->addWidget($widget); + } + + /** + * Display a student's corrected solution for a single exercise. + */ + public function view_solution_action() + { + $exercise_id = Request::int('exercise_id'); + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireViewPermission($assignment, $exercise_id); + + $solver_id = $GLOBALS['user']->id; + $released = $assignment->releaseStatus($solver_id); + + if ($released < VipsAssignment::RELEASE_STATUS_CORRECTIONS) { + // the assignment is not finished or not yet released + PageLayout::postError(_('Die Korrekturen des Aufgabenblatts sind nicht freigegeben.')); + $this->redirect($this->url_for('vips/solutions/student_assignment_solutions', compact('assignment_id'))); + return; + } + + $exercise = Exercise::find($exercise_id); + $solution = $assignment->getSolution($solver_id, $exercise_id); + + if (!$solution) { + $solution = new VipsSolution(); + $solution->assignment = $assignment; + } + + // fetch previous and next exercises + $prev_exercise_id = null; + $next_exercise_id = null; + $before_current = true; + + foreach ($assignment->getExerciseRefs($solver_id) as $i => $item) { + if ($item->task_id == $exercise_id) { + $before_current = false; + $exercise_position = $i + 1; + $max_points = $item->points; + } else if ($before_current) { + $prev_exercise_id = $item->task_id; + } else { + $next_exercise_id = $item->task_id; + break; + } + } + + $this->exercise = $exercise; + $this->exercise_position = $exercise_position; + $this->assignment = $assignment; + $this->solution = $solution; + $this->show_solution = $released == VipsAssignment::RELEASE_STATUS_SAMPLE_SOLUTIONS; + $this->max_points = $max_points; + $this->prev_exercise_id = $prev_exercise_id; + $this->next_exercise_id = $next_exercise_id; + + $widget = new SelectWidget(_('Aufgabenblatt'), $this->url_for('vips/solutions/view_solution', compact('assignment_id')), 'exercise_id'); + + foreach ($assignment->getExerciseRefs($solver_id) as $i => $item) { + $element = new SelectElement($item->task_id, sprintf(_('Aufgabe %d'), $i + 1), $item->task_id === $exercise->id); + $widget->addElement($element); + } + Sidebar::get()->addWidget($widget); + } + + /** + * Restores an archived solution as the current solution. + */ + public function restore_solution_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $solution_id = Request::int('solution_id'); + $solver_id = Request::option('solver_id'); + $view = Request::option('view'); + + $solution = VipsSolution::find($solution_id); + + $exercise_id = $solution->task_id; + $assignment_id = $solution->assignment_id; + $assignment = $solution->assignment; + + VipsModule::requireEditPermission($assignment); + + $assignment->restoreSolution($solution); + PageLayout::postSuccess(_('Die ausgewählte Lösung wurde als aktuelle Version gespeichert.')); + + $this->redirect($this->url_for('vips/solutions/edit_solution', compact('exercise_id', 'assignment_id', 'solver_id', 'view'))); + } + + /** + * Displays a student's event log for an assignment. + */ + public function show_assignment_log_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id'); + + VipsModule::requireEditPermission($assignment); + + $assignment_attempt = $assignment->getAssignmentAttempt($solver_id); + + $this->user = User::find($solver_id); + $this->logs = $assignment_attempt->getLogEntries(); + } + + /** + * Offer to remove users from a group for this assignment. + */ + public function edit_group_dialog_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id'); + $view = Request::option('view'); + + VipsModule::requireEditPermission($assignment); + + $this->group = $assignment->getUserGroup($solver_id); + $this->members = $assignment->getGroupMembers($this->group); + + usort($this->members, function($a, $b) { + return strcoll($a->user->getFullName('no_title_rev'), $b->user->getFullName('no_title_rev')); + }); + + $this->assignment = $assignment; + $this->view = $view; + } + + /** + * Update group assignment for a list of users for this assignment. + */ + public function edit_group_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $group_id = Request::option('group_id'); + $group = VipsGroup::find($group_id); + $user_ids = Request::optionArray('user_ids'); + $view = Request::option('view'); + + VipsModule::requireEditPermission($assignment); + + if ($assignment->isFinished() && $user_ids) { + foreach ($assignment->getGroupMembers($group) as $member) { + if (in_array($member->user_id, $user_ids)) { + $clone = $member->build($member); + $member->end = $assignment->end; + $member->store(); + $clone->start = $assignment->end; + $clone->store(); + } + } + + PageLayout::postSuccess(_('Die ausgewählten Personen wurden aus der Gruppe entfernt.')); + } + + $this->redirect($this->url_for('vips/solutions/assignment_solutions', compact('assignment_id', 'view'))); + } + + /** + * Write a message to selected members for an assignment. + */ + public function write_message_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $user_ids = Request::optionArray('user_ids'); + + VipsModule::requireEditPermission($assignment); + + foreach ($user_ids as $user_id) { + $group = $assignment->getUserGroup($user_id); + + if ($group) { + foreach ($assignment->getGroupMembers($group) as $member) { + $users[] = $member->username; + } + } else { + $users[] = get_username($user_id); + } + } + + $_SESSION['sms_data']['p_rec'] = $users; + $this->redirect(URLHelper::getURL('dispatch.php/messages/write')); + } + + /** + * Stores the lecturer comment and the corrected points for a solution. + */ + public function store_correction_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $solution_id = Request::int('solution_id'); + $solver_id = Request::option('solver_id'); + $view = Request::option('view'); + + $feedback = trim(Request::get('feedback')); + $feedback = Studip\Markup::purifyHtml($feedback); + $file_ids = Request::optionArray('file_ids'); + $corrected = Request::int('corrected', 0); + $reached_points = Request::float('reached_points'); + $max_points = Request::float('max_points'); + + if ($solution_id) { + $solution = VipsSolution::find($solution_id); + } else { + // create dummy empty solution + $solution = new VipsSolution(); + $solution->task_id = Request::int('exercise_id'); + $solution->assignment_id = Request::int('assignment_id'); + $solution->user_id = $solver_id; + } + + $exercise_id = $solution->task_id; + $assignment_id = $solution->assignment_id; + + VipsModule::requireEditPermission($solution->assignment, $exercise_id); + + // let exercise class handle special controls added to the form + $exercise = Exercise::find($exercise_id); + $exercise->correctSolutionAction($this, $solution); + + if (Request::submitted('store_solution')) { + // process lecturer's input + $solution->state = $corrected; + $solution->points = round($reached_points * 2) / 2; + $solution->feedback = $feedback ?: null; + + setlocale(LC_NUMERIC, $_SESSION['_language'] . '.UTF-8'); + + if ($solution->points > $max_points) { + PageLayout::postInfo(sprintf(_('Sie haben Bonuspunkte vergeben: %g von %g.'), $solution->points, $max_points)); + } else if ($solution->points < 0) { + PageLayout::postWarning(sprintf(_('Sie haben eine negative Punktzahl eingegeben: %g von %g.'), $solution->points, $max_points)); + } else if ($solution->points != $reached_points) { + PageLayout::postWarning(sprintf(_('Die eingegebene Punktzahl wurde auf halbe Punkte gerundet: %g.'), $solution->points)); + } + + setlocale(LC_NUMERIC, 'C'); + + $upload = $_FILES['upload'] ?: ['name' => []]; + + if ($solution->isDirty() || count($upload)) { + $solution->grader_id = $GLOBALS['user']->id; + $solution->store(); + + PageLayout::postSuccess(_('Ihre Korrektur wurde gespeichert.')); + } + + $folder = Folder::findTopFolder($solution->id, 'FeedbackFolder', 'response'); + + foreach ($folder->file_refs as $file_ref) { + if (!in_array($file_ref->id, $file_ids) || in_array($file_ref->name, $upload['name'])) { + $file_ref->delete(); + } + } + + FileManager::handleFileUpload($upload, $folder->getTypedFolder()); + } + + // show exercise and correction form again + $this->redirect($this->url_for('vips/solutions/edit_solution', compact('exercise_id', 'assignment_id', 'solver_id', 'view'))); + } + + /** + * Edit an active assignment attempt (update end time). + */ + public function edit_assignment_attempt_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id'); + $view = Request::option('view'); + + VipsModule::requireEditPermission($assignment); + + $this->assignment = $assignment; + $this->assignment_attempt = $assignment->getAssignmentAttempt($solver_id); + $this->solver_id = $solver_id; + $this->view = $view; + } + + /** + * Update an active assignment attempt (store end time). + */ + public function store_assignment_attempt_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $end_time = trim(Request::get('end_time')); + $solver_id = Request::option('solver_id'); + $view = Request::option('view'); + + VipsModule::requireEditPermission($assignment); + + $assignment_attempt = $assignment->getAssignmentAttempt($solver_id); + + if ($assignment_attempt) { + $end_day = date('Y-m-d', $assignment->getUserEndTime($solver_id)); + $end_datetime = DateTime::createFromFormat('Y-m-d H:i:s', $end_day . ' ' . $end_time); + + if ($end_datetime) { + $assignment_attempt->end = strtotime($end_datetime->format('Y-m-d H:i:s')); + $assignment_attempt->store(); + + if ($assignment_attempt->end > $assignment->end) { + PageLayout::postWarning(_('Der Abgabezeitpunkt liegt nach dem Ende der Klausur.')); + } + } else { + PageLayout::postError(_('Der Abgabezeitpunkt ist keine gültige Uhrzeit.')); + } + } + + $this->redirect($this->url_for('vips/solutions/assignment_solutions', compact('assignment_id', 'view'))); + } + + /** + * Deletes the solutions of a student (or all students) and resets the + * assignment attempt. + */ + public function delete_solutions_action() + { + CSRFProtection::verifyUnsafeRequest(); + + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + $solver_id = Request::option('solver_id'); + $view = Request::option('view'); + + VipsModule::requireEditPermission($assignment); + + if ($assignment->type === 'exam') { + if ($solver_id === 'all') { + $assignment->deleteAllSolutions(); + PageLayout::postSuccess(_('Die Klausur wurde zurückgesetzt und alle abgegebenen Lösungen archiviert.')); + } else if ($assignment->isRunning()) { + $assignment->deleteSolutions($solver_id); + PageLayout::postSuccess(_('Die Teilnahme wurde zurückgesetzt und ggf. abgegebene Lösungen archiviert.')); + } + } + + $this->redirect($this->url_for('vips/solutions/assignment_solutions', compact('assignment_id', 'view'))); + } + + + + /** + * Shows all corrected exercises of an exercise sheet, if the status + * of "released" allows that, i.e. is at least 1. + */ + public function student_assignment_solutions_action() + { + $assignment_id = Request::int('assignment_id'); + $assignment = VipsAssignment::find($assignment_id); + + VipsModule::requireViewPermission($assignment); + + $this->assignment = $assignment; + $this->user_id = $GLOBALS['user']->id; + $this->released = $assignment->releaseStatus($this->user_id); + $this->feedback = $assignment->getUserFeedback($this->user_id); + + // Security check -- is assignment really accessible for students? + if ($this->released == VipsAssignment::RELEASE_STATUS_NONE) { + PageLayout::postError(_('Die Korrekturen wurden noch nicht freigegeben.')); + $this->redirect('vips/solutions'); + return; + } + + Helpbar::get()->addPlainText('', + _('Sie können hier die Ergebnisse bzw. die Korrekturen ihrer Aufgaben ansehen.')); + + if ($this->released >= VipsAssignment::RELEASE_STATUS_CORRECTIONS) { + $widget = new ActionsWidget(); + $widget->addLink( + _('Aufgabenblatt drucken'), + $this->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment_id]), + Icon::create('print'), + ['target' => '_blank'] + ); + Sidebar::get()->addWidget($widget); + } + } + + + + /** + * Displays all course participants and all their results (reached points, + * percent, weighted percent) for all tests, blocks and exams plus their + * final grade. + */ + public function participants_overview_action() + { + $this->course_id = Context::getId(); + VipsModule::requireStatus('tutor', $this->course_id); + + $display = Request::option('display', 'points'); + $sort = Request::option('sort', 'name'); + $desc = Request::int('desc'); + $view = Request::option('view'); + $format = Request::option('format'); + + $sem_class = Context::get()->getSemClass(); + $attributes = $this->participants_overview_data($this->course_id, null, $display, $sort, $desc, $view); + + $settings = CourseConfig::get($this->course_id); + $this->has_grades = !empty($settings->VIPS_COURSE_GRADES); + + foreach ($attributes as $key => $value) { + $this->$key = $value; + } + + if ($format == 'csv') { + $columns = [_('Nachname'), _('Vorname'), _('Kennung'), _('Matrikelnr.')]; + + foreach ($this->items as $category => $list) { + foreach ($list as $item) { + $columns[] = $item['name']; + } + } + + $columns[] = _('Summe'); + + if ($display != 'points' && $this->has_grades) { + $columns[] = _('Note'); + } + + $data = [$columns]; + + setlocale(LC_NUMERIC, $_SESSION['_language'] . '.UTF-8'); + + if ($display == 'points' || $this->overall['weighting']) { + if ($display == 'points') { + $row = [_('Maximalpunktzahl'), '', '', '']; + } else { + $row = [_('Gewichtung'), '', '', '']; + } + + foreach ($this->items as $category => $list) { + foreach ($list as $item) { + if ($display == 'points') { + $row[] = sprintf('%.1f', $item['points']); + } else { + $row[] = sprintf('%.1f%%', $item['weighting']); + } + } + } + + if ($display == 'points') { + $row[] = sprintf('%.1f', $this->overall['points']); + } else { + $row[] = '100%'; + + if ($this->has_grades) { + $row[] = ''; + } + } + + $data[] = $row; + } + + foreach ($this->participants as $p) { + $row = [$p['surname'], $p['forename'], $p['username']]; + + if (!$sem_class['studygroup_mode']) { + $row[] = $p['stud_id']; + } else { + $row[] = ''; + } + + foreach ($this->items as $category => $list) { + foreach ($list as $item) { + if ($display == 'points') { + if (isset($p['items'][$category][$item['id']]['points'])) { + $row[] = sprintf('%.1f', $p['items'][$category][$item['id']]['points']); + } else { + $row[] = ''; + } + } else { + if (isset($p['items'][$category][$item['id']]['percent'])) { + $row[] = sprintf('%.1f%%', $p['items'][$category][$item['id']]['percent']); + } else { + $row[] = ''; + } + } + } + } + + if ($display == 'points') { + if (isset($p['overall']['points'])) { + $row[] = sprintf('%.1f', $p['overall']['points']); + } else { + $row[] = ''; + } + } else { + if (isset($p['overall']['weighting'])) { + $row[] = sprintf('%.1f%%', $p['overall']['weighting']); + } else { + $row[] = ''; + } + + if ($this->has_grades) { + $row[] = $p['grade']; + } + } + + $data[] = $row; + } + + setlocale(LC_NUMERIC, 'C'); + + $this->render_csv($data, _('Notenliste.csv')); + } else { + Helpbar::get()->addPlainText('', + _('Diese Seite gibt einen Überblick über die von allen Teilnehmenden erreichten Punkte und ggf. Noten.')); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Ergebnisse'), + $this->url_for('vips/solutions') + ); + $widget->addLink( + _('Punkteübersicht'), + $this->url_for('vips/solutions/participants_overview', ['display' => 'points']) + )->setActive($display == 'points'); + $widget->addLink( + _('Notenübersicht'), + $this->url_for('vips/solutions/participants_overview', ['display' => 'weighting']) + )->setActive($display == 'weighting'); + $widget->addLink( + _('Statistik'), + $this->url_for('vips/solutions/statistics') + ); + Sidebar::get()->addWidget($widget); + + $widget = new ExportWidget(); + $widget->addLink( + _('Liste im CSV-Format exportieren'), + $this->url_for('vips/solutions/participants_overview', ['display' => $display, 'view' => $view, 'sort' => $sort, 'format' => 'csv']), + Icon::create('export') + ); + Sidebar::get()->addWidget($widget); + } + } + + public function statistics_action() + { + $course_id = Context::getId(); + VipsModule::requireStatus('tutor', $course_id); + + $db = DBManager::get(); + + $format = Request::option('format'); + $assignments = []; + + $_assignments = VipsAssignment::findBySQL("range_id = ? AND type IN ('exam', 'practice') ORDER BY start", [$course_id]); + + foreach ($_assignments as $assignment) { + $test_points = 0; + $test_average = 0; + $exercises = []; + + foreach ($assignment->test->exercise_refs as $exercise_ref) { + $exercise = $exercise_ref->exercise; + $exercise_points = (float) $exercise_ref->points; + $exercise_average = 0; + $exercise_correct = 0; + + $exercise_item_count = $exercise->itemCount(); + $exercise_items = array_fill(0, $exercise_item_count, 0); + $exercise_items_c = array_fill(0, $exercise_item_count, 0); + + $query = "SELECT etask_responses.* FROM etask_responses + LEFT JOIN seminar_user USING(user_id) + WHERE etask_responses.assignment_id = $assignment->id + AND etask_responses.task_id = $exercise->id + AND seminar_user.Seminar_id = '$course_id' + AND seminar_user.status = 'autor'"; + + $solution_result = $db->query($query); + $num_solutions = $solution_result->rowCount(); + + foreach ($solution_result as $solution_row) { + $solution = VipsSolution::buildExisting($solution_row); + $solution_points = (float) $solution->points; + + if ($exercise_item_count > 1) { + $items = $exercise->evaluateItems($solution); + $item_scale = $exercise_points / max(count($items), 1); + + foreach ($items as $index => $item) { + $exercise_items[$index] += $item['points'] * $item_scale / $num_solutions; + + if ($item['points'] == 1) { + $exercise_items_c[$index] += 1 / $num_solutions; + } + } + } + + if ($solution_points >= $exercise_points) { + ++$exercise_correct; + } + + $exercise_average += $solution_points / $num_solutions; + } + + $exercises[] = [ + 'id' => $exercise->id, + 'name' => $exercise->title, + 'position' => $exercise_ref->position, + 'points' => $exercise_points, + 'average' => $exercise_average, + 'correct' => $exercise_correct / max($num_solutions, 1), + 'items' => $exercise_items, + 'items_c' => $exercise_items_c + ]; + + $test_points += $exercise_points; + $test_average += $exercise_average; + } + + $assignments[] = [ + 'assignment' => $assignment, + 'points' => $test_points, + 'average' => $test_average, + 'exercises' => $exercises + ]; + } + + $this->assignments = $assignments; + + if ($format == 'csv') { + $columns = [ + _('Titel'), + _('Aufgabe'), + _('Item'), + _('Erreichbare Punkte'), + _('Durchschn. Punkte'), + _('Korrekte Lösungen') + ]; + + $data = [$columns]; + + setlocale(LC_NUMERIC, $_SESSION['_language'] . '.UTF-8'); + + foreach ($assignments as $assignment) { + if (count($assignment['exercises'])) { + $data[] = [ + $assignment['assignment']->test->title, + '', + '', + sprintf('%.1f', $assignment['points']), + sprintf('%.1f', $assignment['average']), + '' + ]; + + foreach ($assignment['exercises'] as $exercise) { + $data[] = [ + $assignment['assignment']->test->title, + $exercise['position'] . '. ' . $exercise['name'], + '', + sprintf('%.1f', $exercise['points']), + sprintf('%.1f', $exercise['average']), + sprintf('%.1f%%', $exercise['correct'] * 100) + ]; + + if (count($exercise['items']) > 1) { + foreach ($exercise['items'] as $index => $item) { + $data[] = [ + $assignment['assignment']->test->title, + $exercise['position'] . '. ' . $exercise['name'], + sprintf(_('Item %d'), $index + 1), + sprintf('%.1f', $exercise['points'] / count($exercise['items'])), + sprintf('%.1f', $item), + sprintf('%.1f%%', $exercise['items_c'][$index] * 100) + ]; + } + } + } + } + } + + setlocale(LC_NUMERIC, 'C'); + + $this->render_csv($data, _('Statistik.csv')); + } else { + Helpbar::get()->addPlainText('', + _('Diese Seite gibt einen Überblick über die im Durchschnitt von allen Teilnehmenden erreichten Punkte ' . + 'sowie den Prozentsatz der vollständig korrekten Lösungen.')); + + $widget = new ViewsWidget(); + $widget->addLink( + _('Ergebnisse'), + $this->url_for('vips/solutions') + ); + $widget->addLink( + _('Punkteübersicht'), + $this->url_for('vips/solutions/participants_overview', ['display' => 'points']) + ); + $widget->addLink( + _('Notenübersicht'), + $this->url_for('vips/solutions/participants_overview', ['display' => 'weighting']) + ); + $widget->addLink( + _('Statistik'), + $this->url_for('vips/solutions/statistics') + )->setActive(); + Sidebar::get()->addWidget($widget); + + $widget = new ExportWidget(); + $widget->addLink( + _('Liste im CSV-Format exportieren'), + $this->url_for('vips/solutions/statistics', ['format' => 'csv']), + Icon::create('export') + ); + Sidebar::get()->addWidget($widget); + } + } + + /** + * Get the internet host name corresponding to a given IP address. + * + * @param string $ip_address host IP address + */ + public function gethostbyaddr(string $ip_address): ?string + { + static $hostname = []; + + if (!array_key_exists($ip_address, $hostname)) { + $hostname[$ip_address] = gethostbyaddr($ip_address); + } + + if ($hostname[$ip_address] !== $ip_address) { + return $hostname[$ip_address]; + } + + return null; + } + + /** + * Get all exercise sheets belonging to course. + */ + private function get_assignments_data($course_id, $user_id, $sort, $desc) + { + $assignments_array = []; + $m_sum_max_points = 0; // holds the maximum points of all exercise sheets + $sum_reached_points = 0; // holds the reached points of all assignments + + // find all assignments + $assignments = VipsAssignment::findByRangeId($course_id); + + usort($assignments, function($a, $b) use ($sort) { + if ($sort === 'title') { + return strcoll($a->test->title, $b->test->title); + } else if ($sort === 'start') { + return strcmp($a->start, $b->start); + } else { + return strcmp($a->end ?: '~', $b->end ?: '~'); + } + }); + + if ($desc) { + $assignments = array_reverse($assignments); + } + + foreach ($assignments as $assignment) { + $max_points = $assignment->test->getTotalPoints(); + + // for students, get reached points + if (!VipsModule::hasStatus('tutor', $course_id)) { + $released = $assignment->releaseStatus($user_id); + + if ($assignment->isVisible($user_id) && $released > 0) { + $reached_points = $assignment->getUserPoints($user_id); + $sum_reached_points += $reached_points; + $m_sum_max_points += $max_points; + } else { + continue; + } + } else { + $released = $assignment->options['released']; + $reached_points = null; + $m_sum_max_points += $max_points; + } + + // count uncorrected solutions + $uncorrected_solutions = $this->count_uncorrected_solutions($assignment->id); + + $assignments_array[] = [ + 'assignment' => $assignment, + 'released' => $released, + 'reached_points' => $reached_points, + 'max_points' => $max_points, + 'uncorrected_solutions' => $uncorrected_solutions + ]; + } + + return [ + 'assignments' => $assignments_array, + 'sum_reached_points' => $sum_reached_points, + 'sum_max_points' => $m_sum_max_points + ]; + } + + private function participants_overview_data($course_id, $param_user_id, $display = null, $sort = null, $desc = null, $view = null) + { + $db = DBManager::get(); + + // fetch all course participants // + + $participants = []; + + $sql = "SELECT user_id + FROM seminar_user + WHERE Seminar_id = ? + AND status NOT IN ('dozent', 'tutor')"; + $result = $db->prepare($sql); + $result->execute([$course_id]); + + foreach ($result as $row) { + $participants[$row['user_id']] = []; + } + + // fetch all assignments with maximum points, assigned to blocks // + // (if appropriate), and with weighting (if appropriate) // + + $types = $view === 'selftest' ? ['selftest'] : ['exam', 'practice']; + + $sql = "SELECT etask_assignments.id, + etask_assignments.type, + etask_tests.title, + etask_assignments.end, + etask_assignments.weight, + etask_assignments.options, + etask_assignments.block_id, + SUM(etask_test_tasks.points) AS points, + etask_blocks.name AS block_name, + etask_blocks.weight AS block_weight + FROM etask_assignments + JOIN etask_tests ON etask_tests.id = etask_assignments.test_id + LEFT JOIN etask_test_tasks + ON etask_test_tasks.test_id = etask_tests.id + LEFT JOIN etask_blocks + ON etask_blocks.id = etask_assignments.block_id + WHERE etask_assignments.range_id = ? + AND etask_assignments.type IN (?) + GROUP BY etask_assignments.id + ORDER BY etask_assignments.type DESC, etask_blocks.name, etask_assignments.start"; + $result = $db->prepare($sql); + $result->execute([$course_id, $types]); + + // the result is ordered by + // * tests + // * blocks + // * exams + // with ascending start points in each category + + $assignments = []; + $items = [ + 'tests' => [], + 'blocks' => [], + 'exams' => [] + ]; + $overall_points = 0; + $overall_weighting = 0; + + // each assignment + foreach ($result as $row) { + $assignment_id = (int) $row['id']; + $test_type = $row['type']; + $test_title = $row['title']; + $points = (float) $row['points']; + $block_id = $row['block_id']; + $block_name = $row['block_name']; + $weighting = (float) $row['weight']; + + $assignment = VipsAssignment::find($assignment_id); + + if (isset($block_id) && $row['block_weight'] !== null) { + $category = 'blocks'; + + // store assignment + $assignments[$assignment_id] = [ + 'assignment' => $assignment, + 'category' => $category, + 'item_id' => $block_id + ]; + + // store item + if (!isset($items[$category][$block_id])) { + $weighting = (float) $row['block_weight']; + + // initialise block + $items[$category][$block_id] = [ + 'id' => $block_id, + 'item' => VipsBlock::find($block_id), + 'name' => $block_name, + 'tooltip' => $block_name.': '.$test_title, + 'points' => 0, + 'weighting' => $weighting + ]; + + // increase overall weighting (just once for each block!) + $overall_weighting += $weighting; + } else { + // extend tooltip for existing block + $items[$category][$block_id]['tooltip'] .= ', '.$test_title; + } + + // increase block's points (for each assignment) + $items[$category][$block_id]['points'] += $points; + + // increase overall points (for each assignment) + $overall_points += $points; + } else { + $category = $test_type === 'exam' ? 'exams' : 'tests'; + + // store assignment + $assignments[$assignment_id] = [ + 'assignment' => $assignment, + 'category' => $category, + 'item_id' => $assignment_id + ]; + + // store item + $items[$category][$assignment_id] = [ + 'id' => $assignment_id, + 'item' => $assignment, + 'name' => $test_title, + 'tooltip' => $test_title, + 'points' => $points, + 'weighting' => $weighting + ]; + + // increase overall points and weighting + $overall_points += $points; + $overall_weighting += $weighting; + } + } + + // overall sum column + $overall = [ + 'points' => $overall_points, + 'weighting' => $overall_weighting + ]; + + if ($overall['weighting'] == 0 && count($assignments) > 0) { + // if weighting is not used, all items weigh equally + $equal_weight = 100 / (count($items['tests']) + count($items['blocks']) + count($items['exams'])); + + foreach ($items as &$list) { + foreach ($list as &$item) { + $item['weighting'] = $equal_weight; + } + } + } + + if (count($assignments) > 0) { + + // fetch all assignments, grouped and summed up by user // + // (assignments that are not solved by any user won't appear) // + + $sql = "SELECT etask_responses.assignment_id, etask_responses.user_id + FROM etask_responses + LEFT JOIN seminar_user + ON seminar_user.user_id = etask_responses.user_id + AND seminar_user.Seminar_id = ? + WHERE etask_responses.assignment_id IN (?) + AND ( + seminar_user.status IS NULL OR + seminar_user.status NOT IN ('dozent', 'tutor') + ) + GROUP BY etask_responses.assignment_id, etask_responses.user_id"; + $result = $db->prepare($sql); + $result->execute([$course_id, array_keys($assignments)]); + + // each assignment + foreach ($result as $row) { + $assignment_id = (int) $row['assignment_id']; + $assignment = $assignments[$assignment_id]['assignment']; + $user_id = $row['user_id']; + $reached_points = $assignment->getUserPoints($user_id); // points in the assignment + + $category = $assignments[$assignment_id]['category']; + $item_id = $assignments[$assignment_id]['item_id']; + + $max_points = $items[$category][$item_id]['points']; // max points for the item + $weighting = $items[$category][$item_id]['weighting']; // item weighting + + // recalc weighting based on item visibility + $sum_weight = $this->participant_weight_sum($items, $user_id); + + if ($sum_weight && ($assignment->isVisible($user_id) || $assignment->getAssignmentAttempt($user_id))) { + $weighting = 100 * $weighting / $sum_weight; + } else { + $weighting = 0; + } + + // compute percent and weighted percent + if ($max_points > 0) { + $percent = round(100 * $reached_points / $max_points, 1); + $weighted_percent = round($weighting * $reached_points / $max_points, 1); + } else { + $percent = 0; + $weighted_percent = 0; + } + + $group = $assignment->getUserGroup($user_id); + + if (isset($group)) { + $members = array_column($assignment->getGroupMembers($group), 'user_id'); + } else { + $members = [$user_id]; + } + + // tests // + + if ($category == 'tests') { + foreach ($members as $member_id) { + if (!isset($participants[$member_id]['items']['tests'][$item_id])) { + // store reached points, percent and weighted percent for this item, for each group member + $participants[$member_id]['items'][$category][$item_id] = [ + 'points' => $reached_points, + 'percent' => $percent + ]; + + if (!isset($participants[$member_id]['overall'])) { + $participants[$member_id]['overall'] = ['points' => 0, 'weighting' => 0]; + } + + // sum up overall points and weighted percent + $participants[$member_id]['overall']['points'] += $reached_points; + $participants[$member_id]['overall']['weighting'] += $weighted_percent; + } + } + } + + // blocks // + + if ($category == 'blocks') { + foreach ($members as $member_id) { + if (!isset($participants[$member_id]['items']['tests_seen'][$assignment_id])) { + $participants[$member_id]['items']['tests_seen'][$assignment_id] = true; + + if (!isset($participants[$member_id]['items']['blocks'][$item_id])) { + $participants[$member_id]['items']['blocks'][$item_id] = ['points' => 0, 'percent' => 0]; + } + + // store reached points, percent and weighted percent for this item, for each group member + $participants[$member_id]['items']['blocks'][$item_id]['points'] += $reached_points; + $participants[$member_id]['items']['blocks'][$item_id]['percent'] += $percent; + + if (!isset($participants[$member_id]['overall'])) { + $participants[$member_id]['overall'] = ['points' => 0, 'weighting' => 0]; + } + + // sum up overall points and weighted percent + $participants[$member_id]['overall']['points'] += $reached_points; + $participants[$member_id]['overall']['weighting'] += $weighted_percent; + } + } + } + + // exams // + + if ($category == 'exams') { + // store reached points, percent and weighted percent for this item + $participants[$user_id]['items'][$category][$item_id] = [ + 'points' => $reached_points, + 'percent' => $percent + ]; + + if (!isset($participants[$user_id]['overall'])) { + $participants[$user_id]['overall'] = ['points' => 0, 'weighting' => 0]; + } + + // sum up overall points and weighted percent + $participants[$user_id]['overall']['points'] += $reached_points; + $participants[$user_id]['overall']['weighting'] += $weighted_percent; + } + } + } + + // if user_id parameter has been passed, delete all participants but the + // requested user (this must take place AFTER all that has been done before + // for to catch all group solutions) + if (isset($param_user_id)) { + $participants = [$param_user_id => $participants[$param_user_id]]; + } + + // get information for each participant + foreach ($participants as $user_id => $rest) { + $user = User::find($user_id); + + $participants[$user_id]['username'] = $user->username; + $participants[$user_id]['forename'] = $user->vorname; + $participants[$user_id]['surname'] = $user->nachname; + $participants[$user_id]['name'] = $user->nachname . ', ' . $user->vorname; + $participants[$user_id]['stud_id'] = $user->matriculation_number; + } + + + // sort participant array // + + $sort_by_name = function($a, $b) { // sort by name + return strcoll($a['name'], $b['name']); + }; + + $sort_by_points = function($a, $b) use ($sort_by_name) { // sort by points (or name, if points are equal) + if ($a['overall']['points'] == $b['overall']['points']) { + return $sort_by_name($a, $b); + } else { + return $a['overall']['points'] < $b['overall']['points'] ? -1 : 1; + } + }; + + $sort_by_grade = function($a, $b) use ($sort_by_name) { // sort by grade (or name, if grade is equal) + if ($a['overall']['weighting'] == $b['overall']['weighting']) { + return $sort_by_name($a, $b); + } else { + return $a['overall']['weighting'] < $b['overall']['weighting'] ? -1 : 1; + } + }; + + switch ($sort) { + case 'sum': // sort by sum row + if ($display == 'points') { + uasort($participants, $sort_by_points); + } else { + uasort($participants, $sort_by_grade); + } + break; + + case 'grade': // sort by grade (or name, if grade is equal) + uasort($participants, $sort_by_grade); + break; + + case 'name': // sort by name + default: + uasort($participants, $sort_by_name); + } + + if ($desc) { + $participants = array_reverse($participants, true); + } + + // fetch grades from database + $settings = CourseConfig::get($course_id); + + // grading is used + if ($settings->VIPS_COURSE_GRADES) { + foreach ($participants as $user_id => $participant) { + $participants[$user_id]['grade'] = '5,0'; + + if (isset($participant['overall'])) { + foreach ($settings->VIPS_COURSE_GRADES as $g) { + $grade = $g['grade']; + $percent = $g['percent']; + $comment = $g['comment']; + + if ($participant['overall']['weighting'] >= $percent) { + $participants[$user_id]['grade'] = $grade; + $participants[$user_id]['grade_comment'] = $comment; + break; + } + } + } + } + } + + return [ + 'display' => $display, + 'sort' => $sort, + 'desc' => $desc, + 'view' => $view, + 'items' => $items, + 'overall' => $overall, + 'participants' => $participants + ]; + } + + private function participant_weight_sum($items, $user_id) + { + static $weight_sum = []; + + if (!array_key_exists($user_id, $weight_sum)) { + $weight_sum[$user_id] = 0; + + foreach ($items as $list) { + foreach ($list as $item) { + if ($item['item']->isVisible($user_id) || $item['item']->getAssignmentAttempt($user_id)) { + $weight_sum[$user_id] += $item['weighting']; + } + } + } + } + + return $weight_sum[$user_id]; + } + + /** + * Get all solutions for an assignment. + * + * @param object $assignment The assignment + * @param string|bool $view If set to the empty string, only users with solutions are + * returned. If set to string all, virtually + * all course participants (including those who have + * not delivered any solution) are returned. + * @return Array An array consisting of three arrays, namely 'solvers' + * (containing all single solvers and groups), 'exercises' + * (containing all exercises in the assignment) and 'solutions' + * (containing all solvers and their solved exercises). + */ + private function get_solutions($assignment, $view) + { + // get exercises // + + $exercises = []; + + foreach ($assignment->test->exercise_refs as $exercise_ref) { + $exercise_id = (int) $exercise_ref->task_id; + + $exercises[$exercise_id] = [ + 'id' => $exercise_id, + 'title' => $exercise_ref->exercise->title, + 'type' => $exercise_ref->exercise->type, + 'position' => (int) $exercise_ref->position, + 'points' => (float) $exercise_ref->points + ]; + } + + // get course participants // + + $solvers = []; + $tutors = []; + + foreach ($assignment->course->members as $member) { + $user_id = $member->user_id; + $status = $member->status; + + // don't include tutors and lecturers + if ($status == 'tutor' || $status == 'dozent') { + $tutors[$user_id] = $status; + } else { + $solvers[$user_id] = [ + 'type' => 'single', + 'id' => $user_id, + 'user_id' => $user_id + ]; + } + } + + // get assignment attempts // + + foreach ($assignment->assignment_attempts as $attempt) { + $user_id = $attempt->user_id; + + $solvers[$user_id] = [ + 'type' => 'single', + 'id' => $user_id, + 'user_id' => $user_id + ]; + } + + // get solutions // + + $solutions = []; + + foreach ($assignment->solutions as $solution) { + $exercise_id = (int) $solution->task_id; + $user_id = $solution->user_id; + + $solutions[$user_id][$exercise_id] = [ + 'id' => (int) $solution->id, + 'exercise_id' => $exercise_id, + 'user_id' => $user_id, + 'time' => $solution->mkdate, + 'corrected' => (boolean) $solution->state, + 'points' => (float) $solution->points, + 'grader_id' => $solution->grader_id, + 'feedback' => $solution->feedback, + 'uploads' => $solution->folder && count($solution->folder->file_refs) + ]; + + // solver may be a non-participant (and must not be a tutor) + if (!isset($solvers[$user_id]) && !isset($tutors[$user_id])) { + $solvers[$user_id] = [ + 'type' => 'single', + 'id' => $user_id, + 'user_id' => $user_id + ]; + } + } + + /// NOTE: $solvers now *additionally* contains all students which have + /// submitted a solution + + // get groups // + + $groups = []; + + if ($assignment->hasGroupSolutions()) { + $all_groups = VipsGroup::findBySQL('range_id = ? ORDER BY name', [$assignment->range_id]); + + foreach ($all_groups as $group) { + $members = $assignment->getGroupMembers($group); + + foreach ($members as $member) { + $group_id = (int) $group->id; + $user_id = $member->user_id; + + if (!isset($solvers[$user_id])) { + // add group member to $solvers + $solvers[$user_id] = [ + 'type' => 'group_member', + 'id' => $user_id, + 'user_id' => $user_id + ]; + } else { + // update type for existing solvers + $solvers[$user_id]['type'] = 'group_member'; + } + + if (!isset($groups[$group_id])) { + $groups[$group_id] = [ + 'type' => 'group', + 'id' => $group_id, + 'user_id' => $user_id, + 'name' => $group->name, + 'members' => [] + ]; + } + + // store which user is member of which group (user_id => group_id) + $map_user_to_group[$user_id] = $group_id; + } + } + } + + /// NOTE: $solvers now *additionally* contains group members (if applicable) + + if (count($solvers)) { + $result = User::findMany(array_keys($solvers)); + + // get user names + foreach ($result as $user) { + $solvers[$user->id]['username'] = $user->username; + $solvers[$user->id]['forename'] = $user->vorname; + $solvers[$user->id]['surname'] = $user->nachname; + $solvers[$user->id]['name'] = $user->nachname . ', ' . $user->vorname; + $solvers[$user->id]['stud_id'] = $user->matriculation_number; + } + + uasort($solvers, function($a, $b) { + return strcoll($a['name'], $b['name']); + }); + } + + // add groups to $solvers array // + + foreach ($groups as $group_id => $group) { + $solvers[$group_id] = $group; + } + + // sort single solvers to groups // + + foreach ($solvers as $solver_id => $solver) { + if ($solver['type'] == 'group_member') { + $group_id = $map_user_to_group[$solver_id]; + + $solvers[$group_id]['members'][$solver_id] = $solver; // store solver as group member + unset($solvers[$solver_id]); // delete him as single solver + } + } + + // change solution user ids to group ids // + + foreach ($solutions as $solver_id => $exercise_solutions) { + if (isset($map_user_to_group[$solver_id])) { + $group_id = $map_user_to_group[$solver_id]; + + foreach ($exercise_solutions as $exercise_id => $solution) { + // always store most recent solution + if (!isset($solutions[$group_id][$exercise_id]) || $solution['time'] > $solutions[$group_id][$exercise_id]['time']) { + $solutions[$group_id][$exercise_id] = $solution; // store solution as group solution + } + unset($solutions[$solver_id][$exercise_id]); // delete single-solver-solution + } + } + } + + // remove hidden solver entries // + + if ($assignment->type !== 'exam') { + foreach ($solvers as $solver_id => $solver) { + if (!isset($solutions[$solver_id])) { // has no solutions + if (!$view || $view == 'todo') { + unset($solvers[$solver_id]); + } + } else if ($view == 'todo') { + foreach ($solutions[$solver_id] as $solution) { + if (!$solution['corrected']) { + continue 2; + } + } + + unset($solvers[$solver_id]); + } + } + } + + return [ + 'solvers' => $solvers, // ordered by name + 'exercises' => $exercises, // ordered by position + 'solutions' => $solutions // first single solvers then groups, furthermore unordered + ]; + } + + /** + * Counts uncorrected solutions for a assignment. + * + * @param $assignment_id The assignment id + * @return null if there does not exist any solution at all, else + * the number of uncorrected solutions + */ + private function count_uncorrected_solutions($assignment_id) + { + $db = DBManager::get(); + + $assignment = VipsAssignment::find($assignment_id); + $course_id = $assignment->range_id; + + // get all corrected and uncorrected solutions + $sql = "SELECT etask_responses.task_id, + etask_responses.user_id, + etask_responses.state + FROM etask_responses + LEFT JOIN seminar_user + ON seminar_user.user_id = etask_responses.user_id + AND seminar_user.Seminar_id = ? + WHERE etask_responses.assignment_id = ? + AND ( + seminar_user.status IS NULL OR + seminar_user.status NOT IN ('dozent', 'tutor') + ) + ORDER BY etask_responses.mkdate DESC"; + $result = $db->prepare($sql); + $result->execute([$course_id, $assignment_id]); + + // no solutions at all + if ($result->rowCount() == 0) { + return null; + } + + // count uncorrected solutions + $uncorrected_solutions = 0; + $solution = []; + $group = []; + + foreach ($result as $row) { + $exercise_id = (int) $row['task_id']; + $user_id = $row['user_id']; + $corrected = (boolean) $row['state']; + + if (!array_key_exists($user_id, $group)) { + $group[$user_id] = $assignment->getUserGroup($user_id); + } + + if (!array_key_exists($exercise_id . '_' . $user_id, $solution)) { + if (isset($group[$user_id])) { + $members = array_column($assignment->getGroupMembers($group[$user_id]), 'user_id'); + } else { + $members = [$user_id]; + } + + foreach ($members as $user_id) { + $solution[$exercise_id . '_' . $user_id] = true; + } + + if (!$corrected) { + $uncorrected_solutions++; + } + } + } + + return $uncorrected_solutions; + } + + /** + * Return the appropriate CSS class for sortable column (if any). + * + * @param boolean $sort sort by this column + * @param boolean $desc set sort direction + */ + public function sort_class(bool $sort, ?bool $desc): string + { + return $sort ? ($desc ? 'sortdesc' : 'sortasc') : ''; + } +} diff --git a/app/views/vips/admin/edit_block.php b/app/views/vips/admin/edit_block.php new file mode 100644 index 0000000..70710cf --- /dev/null +++ b/app/views/vips/admin/edit_block.php @@ -0,0 +1,47 @@ + +
+ + + id): ?> + + + + + + + + + +
+ +
+
diff --git a/app/views/vips/admin/edit_grades.php b/app/views/vips/admin/edit_grades.php new file mode 100644 index 0000000..d83db43 --- /dev/null +++ b/app/views/vips/admin/edit_grades.php @@ -0,0 +1,56 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ % + + > +
+ +
+ +
+ +
+
+ + diff --git a/app/views/vips/config/index.php b/app/views/vips/config/index.php new file mode 100644 index 0000000..d4b1f69 --- /dev/null +++ b/app/views/vips/config/index.php @@ -0,0 +1,90 @@ + +
+ + + +
+ + + + +
+ +
+ + + +
+ +
+ + + + + + + + + + + + VIPS_EXAM_ROOMS ?: [] as $room => $ip_range): ?> + + + + + + + + + + + + + + + + + +
+ + + + render_partial('vips/sheets/ip_range_tooltip'), false, true) ?> + + +
+ + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Eintrag löschen')]) ?> +
+ + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Eintrag löschen')]) ?> +
+ 'add_dynamic_row']) ?> +
+ + +
+ +
+ +
+
diff --git a/app/views/vips/config/pending_assignments.php b/app/views/vips/config/pending_assignments.php new file mode 100644 index 0000000..8a21874 --- /dev/null +++ b/app/views/vips/config/pending_assignments.php @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+ + + + + + + + + +
+ + getTypeIcon() ?> + test->title) ?> + + isRunning() && !$assignment->active): ?> + () + + + start) ?> + + end) ?> + + test->user->getFullName('no_title_rev')) ?> + + + course->name) ?> + +
+ diff --git a/app/views/vips/exam_mode/index.php b/app/views/vips/exam_mode/index.php new file mode 100644 index 0000000..50bf0a6 --- /dev/null +++ b/app/views/vips/exam_mode/index.php @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + $course_name) : ?> + getIconNavigation($course_id, null, null) ?> + + + + + + + + + +
+ +
+ getImageTag(Avatar::SMALL) ?> + + + + + + + getImage()->asImg($nav->getLinkAttributes()) ?> + +
+ + + + diff --git a/app/views/vips/exercises/ClozeTask/correct.php b/app/views/vips/exercises/ClozeTask/correct.php new file mode 100644 index 0000000..8e4aac1 --- /dev/null +++ b/app/views/vips/exercises/ClozeTask/correct.php @@ -0,0 +1,42 @@ + +
+ asImg(['class' => 'correction_inline', 'title' => _('richtig')]) ?>asImg(['class' => 'correction_inline', 'title' => _('fast richtig')]) ?>asImg(['class' => 'correction_inline', 'title' => _('falsch')]) ?>asImg(['class' => 'correction_inline', 'title' => _('unbekannte Antwort')]) ?>correctAnswers($blank))) ?> +
+ +render_partial('exercises/evaluation_mode_info', ['evaluation_mode' => false]) ?> diff --git a/app/views/vips/exercises/ClozeTask/edit.php b/app/views/vips/exercises/ClozeTask/edit.php new file mode 100644 index 0000000..51c04a9 --- /dev/null +++ b/app/views/vips/exercises/ClozeTask/edit.php @@ -0,0 +1,67 @@ + +%s:
[[ ... ]]

%s:
[[ ... | ... | ... ]]

%s:
[[ ... | ... | *... ]]

%s:
[[: ... | ... | *... ]]

', + _('Lücke hinzufügen'), _('Mehrere Lösungen mit | trennen'), _('Falsche Antworten mit * markieren'), _('Auswahl aus Liste statt Eingabe')) ?> + + + + + +
+ + + + + +
diff --git a/app/views/vips/exercises/ClozeTask/print.php b/app/views/vips/exercises/ClozeTask/print.php new file mode 100644 index 0000000..fce4bc4 --- /dev/null +++ b/app/views/vips/exercises/ClozeTask/print.php @@ -0,0 +1,62 @@ + +
+     asImg(['title' => _('richtig')]) ?>asImg(['title' => _('fast richtig')]) ?>asImg(['title' => _('falsch')]) ?> 'vertical-align: text-bottom;']) ?> getInputWidth($blank)) ?>correctAnswers($blank))) ?> +
+ +interactionType() === 'drag'): ?> +
+ + + + + +
+ +
    + availableAnswers($solution) as $item): ?> +
  1. + +
  2. + +
+ + +render_partial('exercises/evaluation_mode_info', ['evaluation_mode' => false]) ?> diff --git a/app/views/vips/exercises/ClozeTask/solve.php b/app/views/vips/exercises/ClozeTask/solve.php new file mode 100644 index 0000000..bc17a03 --- /dev/null +++ b/app/views/vips/exercises/ClozeTask/solve.php @@ -0,0 +1,46 @@ + +
+ + + + + + +
+ +interactionType() === 'drag'): ?> + + availableAnswers($solution) as $item): ?> + + + + diff --git a/app/views/vips/exercises/ClozeTask/xml.php b/app/views/vips/exercises/ClozeTask/xml.php new file mode 100644 index 0000000..65ebb98 --- /dev/null +++ b/app/views/vips/exercises/ClozeTask/xml.php @@ -0,0 +1,63 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + + + task['text']) as $blank => $text): ?> + + task['answers'][$blank])): ?> + isSelect($blank, false)): ?> select="true"> + task['answers'][$blank] as $answer): ?> + + + + + + + task['input_width'])): ?> + + + + + task['compare'])): ?> + + + task['compare'] === 'numeric'): ?> + + task['epsilon'] ?> + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/MatchingTask/correct.php b/app/views/vips/exercises/MatchingTask/correct.php new file mode 100644 index 0000000..64faca6 --- /dev/null +++ b/app/views/vips/exercises/MatchingTask/correct.php @@ -0,0 +1,88 @@ + +sortAnswersById(); ?> + + + + + + + + + + + + + + + + task['groups'] as $i => $group) : ?> + + + + + + + + + + + +
+ + + + + +
+
+ +
+
+ task['answers'] as $answer): ?> + +
+ + + isCorrectAnswer($answer, $i)): ?> + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + +
+ + +
+ correctAnswers($i) as $correct_answer): ?> +
+ +
+ +
+ +
+ +
+ +task['answers'] as $answer): ?> + +
+ + + id): ?> + isCorrectAnswer($answer, -1)): ?> + asImg(['class' => 'correction_inline', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_inline', 'title' => _('falsch')]) ?> + + +
+ + diff --git a/app/views/vips/exercises/MatchingTask/edit.php b/app/views/vips/exercises/MatchingTask/edit.php new file mode 100644 index 0000000..3ddc8f5 --- /dev/null +++ b/app/views/vips/exercises/MatchingTask/edit.php @@ -0,0 +1,117 @@ + + + + + + + + + + + + + task['groups'] as $i => $group): ?> + flexibleInputSize($group) ?> + + + + + + + + + + + + + + + + +
+ + + +
+ render_partial('exercises/flexible_input', ['name' => "default[$i]", 'value' => $group, 'size' => $size]) ?> + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Zuordnung löschen')]) ?> + + + task['answers'] as $answer): ?> + + flexibleInputSize($answer['text']) ?> + +
+ render_partial('exercises/flexible_input', ['name' => "answer[$i][$j]", 'value' => $answer['text'], 'size' => $size]) ?> + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + + +
+ render_partial('exercises/flexible_input', ['data_name' => "answer[$i]", 'size' => 'small']) ?> + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + 'add_dynamic_row rh_add_answer']) ?> +
+ render_partial('exercises/flexible_input', ['data_name' => 'default', 'size' => 'small']) ?> + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Zuordnung löschen')]) ?> + +
+ render_partial('exercises/flexible_input', ['data_name' => ':answer', 'size' => 'small']) ?> + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + 'add_dynamic_row rh_add_answer']) ?> +
+ 'add_dynamic_row']) ?> +
+ +
+ + +
+ +
+ task['answers'] as $answer): ?> + + flexibleInputSize($answer['text']) ?> + +
+ + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Distraktor löschen')]) ?> +
+ + + +
+ + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Distraktor löschen')]) ?> +
+ + 'add_dynamic_row']) ?> +
diff --git a/app/views/vips/exercises/MatchingTask/print.php b/app/views/vips/exercises/MatchingTask/print.php new file mode 100644 index 0000000..a9617df --- /dev/null +++ b/app/views/vips/exercises/MatchingTask/print.php @@ -0,0 +1,95 @@ + +sortAnswersById(); ?> + + + + + + + + + + + + + + + + task['groups'] as $i => $group) : ?> + + + + + + + + + + + +
+ + + + + +
+
+ +
+
+ task['answers'] as $answer): ?> + +
+ + + + isCorrectAnswer($answer, $i)) : ?> + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + +
+ + +
+ correctAnswers($i) as $correct_answer): ?> +
+ +
+ +
+ +
+ + + + + +
+ +
    + task['answers'] as $answer): ?> + +
  1. + + + id && $print_correction): ?> + isCorrectAnswer($answer, -1)): ?> + asImg(['title' => _('richtig')]) ?> + + asImg(['title' => _('falsch')]) ?> + + +
  2. + + +
diff --git a/app/views/vips/exercises/MatchingTask/solve.php b/app/views/vips/exercises/MatchingTask/solve.php new file mode 100644 index 0000000..8202819 --- /dev/null +++ b/app/views/vips/exercises/MatchingTask/solve.php @@ -0,0 +1,38 @@ + +sortAnswersById(); ?> + + + task['groups'] as $i => $group): ?> + + + + + + + + +
+ + + task['answers'] as $answer): ?> + +
+ + +
+ + +
+ task['answers'] as $answer): ?> + +
+ + +
+ + +
diff --git a/app/views/vips/exercises/MatchingTask/xml.php b/app/views/vips/exercises/MatchingTask/xml.php new file mode 100644 index 0000000..41d35af --- /dev/null +++ b/app/views/vips/exercises/MatchingTask/xml.php @@ -0,0 +1,50 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + + + task['groups'] as $group): ?> + + + + + + + task['answers'] as $answer): ?> + + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/MatrixChoiceTask/correct.php b/app/views/vips/exercises/MatrixChoiceTask/correct.php new file mode 100644 index 0000000..1b4f8ba --- /dev/null +++ b/app/views/vips/exercises/MatrixChoiceTask/correct.php @@ -0,0 +1,42 @@ + + + task['answers'] as $key => $entry): ?> + + + + + + +
+ + + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + + + task['choices'] + $optional_choice as $val => $label): ?> + + + 'margin-left: 1ex;']) ?> + + 'margin-left: 1ex;']) ?> + + + + +
+ +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/MatrixChoiceTask/edit.php b/app/views/vips/exercises/MatrixChoiceTask/edit.php new file mode 100644 index 0000000..946a217 --- /dev/null +++ b/app/views/vips/exercises/MatrixChoiceTask/edit.php @@ -0,0 +1,91 @@ + +
+ +
+ +
+ task['choices'] as $i => $choice): ?> + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Auswahlmöglichkeit löschen')]) ?> + / + + + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Auswahlmöglichkeit löschen')]) ?> + / + + + asInput(['class' => 'add_dynamic_row', 'title' => _('Auswahlmöglichkeit hinzufügen')]) ?> +
+ + + +
+ +
+ +
+ task['answers'] as $i => $answer): ?> + flexibleInputSize($answer['text']); ?> + +
+ + + + task['choices'] as $val => $choice): ?> + + + + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Frage löschen')]) ?> +
+ + +
+ + + + task['choices'] as $val => $choice): ?> + + + + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Frage löschen')]) ?> +
+ + 'add_dynamic_row']) ?> +
+ +
+ +
diff --git a/app/views/vips/exercises/MatrixChoiceTask/print.php b/app/views/vips/exercises/MatrixChoiceTask/print.php new file mode 100644 index 0000000..314730f --- /dev/null +++ b/app/views/vips/exercises/MatrixChoiceTask/print.php @@ -0,0 +1,41 @@ + + + task['answers'] as $key => $entry): ?> + + + + + + +
+ + + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + + + task['choices'] + $optional_choice as $val => $label): ?> + + + 'margin-left: 1ex;']) ?> + + 'margin-left: 1ex;']) ?> + + + + +
+ +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/MatrixChoiceTask/solve.php b/app/views/vips/exercises/MatrixChoiceTask/solve.php new file mode 100644 index 0000000..5b76764 --- /dev/null +++ b/app/views/vips/exercises/MatrixChoiceTask/solve.php @@ -0,0 +1,27 @@ + + + task['answers'] as $key => $entry): ?> + + + + + + +
+ + + task['choices'] + $optional_choice as $val => $label): ?> + + +
+ +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/MatrixChoiceTask/xml.php b/app/views/vips/exercises/MatrixChoiceTask/xml.php new file mode 100644 index 0000000..2b8238b --- /dev/null +++ b/app/views/vips/exercises/MatrixChoiceTask/xml.php @@ -0,0 +1,51 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + + + task['choices'] + $optional_choice as $key => $choice): ?> + + + + + + + task['answers'] as $answer): ?> + + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/MultipleChoiceTask/correct.php b/app/views/vips/exercises/MultipleChoiceTask/correct.php new file mode 100644 index 0000000..2f2a6dc --- /dev/null +++ b/app/views/vips/exercises/MultipleChoiceTask/correct.php @@ -0,0 +1,32 @@ + +
+ task['answers'] as $key => $entry): ?> +
+ + + + + + + + + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + +
+ +
+ +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/MultipleChoiceTask/edit.php b/app/views/vips/exercises/MultipleChoiceTask/edit.php new file mode 100644 index 0000000..c1ab69f --- /dev/null +++ b/app/views/vips/exercises/MultipleChoiceTask/edit.php @@ -0,0 +1,46 @@ + +
+ +
+ +
+ task['answers'] as $i => $answer): ?> + flexibleInputSize($answer['text']); ?> + +
+ + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + +
+ + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + 'add_dynamic_row']) ?> +
+ +
+ +
diff --git a/app/views/vips/exercises/MultipleChoiceTask/print.php b/app/views/vips/exercises/MultipleChoiceTask/print.php new file mode 100644 index 0000000..d352f17 --- /dev/null +++ b/app/views/vips/exercises/MultipleChoiceTask/print.php @@ -0,0 +1,30 @@ + +
+ task['answers'] as $key => $entry): ?> +
+ + + + + + + + + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + +
+ +
+ +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/MultipleChoiceTask/solve.php b/app/views/vips/exercises/MultipleChoiceTask/solve.php new file mode 100644 index 0000000..3c716e4 --- /dev/null +++ b/app/views/vips/exercises/MultipleChoiceTask/solve.php @@ -0,0 +1,13 @@ + +task['answers'] as $key => $entry): ?> + + + +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/MultipleChoiceTask/xml.php b/app/views/vips/exercises/MultipleChoiceTask/xml.php new file mode 100644 index 0000000..4c1389d --- /dev/null +++ b/app/views/vips/exercises/MultipleChoiceTask/xml.php @@ -0,0 +1,43 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + + + task['answers'] as $answer): ?> + + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/SequenceTask/correct.php b/app/views/vips/exercises/SequenceTask/correct.php new file mode 100644 index 0000000..72cfba5 --- /dev/null +++ b/app/views/vips/exercises/SequenceTask/correct.php @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + $id): ?> + task['answers'] as $i => $answer): ?> + + task['compare'] === 'sequence'): ?> +
+ +
+ + +
+ + } + asImg(['title' => _('richtig')]) ?> + + } + asImg(['title' => _('falsch')]) ?> + +
+ + task['compare'] === 'position'): ?> +
+ + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + +
+ +
+ +
+ + + + + + orderedAnswers($response) as $answer): ?> +
+ +
+ + +
+ task['answers'] as $answer): ?> +
+ +
+ +
diff --git a/app/views/vips/exercises/SequenceTask/edit.php b/app/views/vips/exercises/SequenceTask/edit.php new file mode 100644 index 0000000..d8d61cc --- /dev/null +++ b/app/views/vips/exercises/SequenceTask/edit.php @@ -0,0 +1,50 @@ + +
+ +
+ +
+ task['answers'] as $answer): ?> + flexibleInputSize($answer['text']); ?> + +
+ + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + +
+ + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + 'add_dynamic_row']) ?> +
+ + diff --git a/app/views/vips/exercises/SequenceTask/print.php b/app/views/vips/exercises/SequenceTask/print.php new file mode 100644 index 0000000..1ccb76d --- /dev/null +++ b/app/views/vips/exercises/SequenceTask/print.php @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+
    + + $id): ?> + task['answers'] as $i => $answer): ?> + + task['compare'] === 'sequence'): ?> +
  1. + +
  2. + + +
    + + } + asImg(['title' => _('richtig')]) ?> + + } + asImg(['title' => _('falsch')]) ?> + +
    + + task['compare'] === 'position'): ?> +
  3. + + + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + +
  4. + +
  5. + +
  6. + + + + + + orderedAnswers($response) as $answer): ?> +
  7. + +
  8. + + +
+
+
    + task['answers'] as $answer): ?> +
  1. + +
  2. + +
+
diff --git a/app/views/vips/exercises/SequenceTask/solve.php b/app/views/vips/exercises/SequenceTask/solve.php new file mode 100644 index 0000000..9ffe605 --- /dev/null +++ b/app/views/vips/exercises/SequenceTask/solve.php @@ -0,0 +1,14 @@ + +
+ orderedAnswers($response) as $answer): ?> +
+ + +
+ +
diff --git a/app/views/vips/exercises/SequenceTask/xml.php b/app/views/vips/exercises/SequenceTask/xml.php new file mode 100644 index 0000000..244784c --- /dev/null +++ b/app/views/vips/exercises/SequenceTask/xml.php @@ -0,0 +1,48 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + + + task['answers'] as $answer): ?> + + + + + + task['compare'])): ?> + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/SingleChoiceTask/correct.php b/app/views/vips/exercises/SingleChoiceTask/correct.php new file mode 100644 index 0000000..cecc743 --- /dev/null +++ b/app/views/vips/exercises/SingleChoiceTask/correct.php @@ -0,0 +1,41 @@ + +task as $group => $task): ?> +
> + + + +
+ +
+ $entry): ?> +
+ + + + + + + + + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + +
+ +
+ + +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/SingleChoiceTask/edit.php b/app/views/vips/exercises/SingleChoiceTask/edit.php new file mode 100644 index 0000000..bd368c7 --- /dev/null +++ b/app/views/vips/exercises/SingleChoiceTask/edit.php @@ -0,0 +1,86 @@ + + + +
+ +
+ +
+ task as $j => $task): ?> +
+ + + $answer): ?> + flexibleInputSize($answer['text']); ?> + +
+ + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + +
+ + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + 'add_dynamic_row']) ?> + 'delete_dynamic_row']) ?> +
+ + +
+ + +
+ + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + 'add_dynamic_row']) ?> + 'delete_dynamic_row']) ?> +
+ + 'add_dynamic_row']) ?> +
+ +
+ +
diff --git a/app/views/vips/exercises/SingleChoiceTask/print.php b/app/views/vips/exercises/SingleChoiceTask/print.php new file mode 100644 index 0000000..a61bc84 --- /dev/null +++ b/app/views/vips/exercises/SingleChoiceTask/print.php @@ -0,0 +1,41 @@ + +task as $group => $task): ?> +
> + + + +
+ +
+ $entry): ?> +
+ + + + + + + + + + + + asImg(['class' => 'correction_marker', 'title' => _('richtig')]) ?> + + asImg(['class' => 'correction_marker', 'title' => _('falsch')]) ?> + + + +
+ +
+ + +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/SingleChoiceTask/solve.php b/app/views/vips/exercises/SingleChoiceTask/solve.php new file mode 100644 index 0000000..70689bf --- /dev/null +++ b/app/views/vips/exercises/SingleChoiceTask/solve.php @@ -0,0 +1,23 @@ + +task as $group => $task): ?> +
> + + + +
+ + $entry): ?> + + + + +render_partial('exercises/evaluation_mode_info') ?> diff --git a/app/views/vips/exercises/SingleChoiceTask/xml.php b/app/views/vips/exercises/SingleChoiceTask/xml.php new file mode 100644 index 0000000..423a183 --- /dev/null +++ b/app/views/vips/exercises/SingleChoiceTask/xml.php @@ -0,0 +1,51 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + task as $group => $task): ?> + + + + + + + + $answer): ?> + default="true"> + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/TextLineTask/correct.php b/app/views/vips/exercises/TextLineTask/correct.php new file mode 100644 index 0000000..27bcefc --- /dev/null +++ b/app/views/vips/exercises/TextLineTask/correct.php @@ -0,0 +1,37 @@ + +id): ?> +
+ +
+ + + + + asImg(['title' => _('richtig')]) ?> + + asImg(['title' => _('fast richtig')]) ?> + + asImg(['title' => _('falsch')]) ?> + + asImg(['title' => _('unbekannte Antwort')]) ?> + + + +correctAnswers()): ?> +
+ + + + correctAnswers())) ?> + +
+ diff --git a/app/views/vips/exercises/TextLineTask/edit.php b/app/views/vips/exercises/TextLineTask/edit.php new file mode 100644 index 0000000..3d2af0a --- /dev/null +++ b/app/views/vips/exercises/TextLineTask/edit.php @@ -0,0 +1,80 @@ + +
+ +
+ +
+ task['answers'] as $i => $answer): ?> +
+ + + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + +
+ + + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Antwort löschen')]) ?> +
+ + 'add_dynamic_row']) ?> +
+ + + + diff --git a/app/views/vips/exercises/TextLineTask/print.php b/app/views/vips/exercises/TextLineTask/print.php new file mode 100644 index 0000000..3bfbc2e --- /dev/null +++ b/app/views/vips/exercises/TextLineTask/print.php @@ -0,0 +1,35 @@ + +id) : ?> + + + + + asImg(['title' => _('richtig')]) ?> + + asImg(['title' => _('fast richtig')]) ?> + + asImg(['title' => _('falsch')]) ?> + + + +
+ + +correctAnswers()) : ?> +
+ + + + correctAnswers())) ?> + +
+ diff --git a/app/views/vips/exercises/TextLineTask/solve.php b/app/views/vips/exercises/TextLineTask/solve.php new file mode 100644 index 0000000..8fab98a --- /dev/null +++ b/app/views/vips/exercises/TextLineTask/solve.php @@ -0,0 +1,9 @@ + + diff --git a/app/views/vips/exercises/TextLineTask/xml.php b/app/views/vips/exercises/TextLineTask/xml.php new file mode 100644 index 0000000..d4b46c0 --- /dev/null +++ b/app/views/vips/exercises/TextLineTask/xml.php @@ -0,0 +1,53 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + + + task['answers'] as $answer): ?> + + + + + + task['compare'])): ?> + + + task['compare'] === 'numeric'): ?> + + task['epsilon'] ?> + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/TextTask/correct.php b/app/views/vips/exercises/TextTask/correct.php new file mode 100644 index 0000000..bdd99a6 --- /dev/null +++ b/app/views/vips/exercises/TextTask/correct.php @@ -0,0 +1,184 @@ + +getLayout() !== 'none' && $response[0] != ''): ?> +
+ + +
+ + getLayout() === 'markup'): ?> + + getLayout() === 'code'): ?> + + + + + + + + + _('Wollen Sie die kommentierte Lösung löschen?')]) ?> + + commented_solution): ?> + +
+ +
+ +
+ commented_solution) ?> +
+ + + +
+ commented_solution) ?> +
+ +
+ +
+
+ getLayout() === 'text'): ?> + + getLayout() === 'markup'): ?> + + getLayout() === 'code'): ?> +
+ + +
+ + + 'edit_solution']) ?> + + getLayout() === 'code'): ?> + + 'vips_file_download']) ?> + + +
+ + task['template'] != ''): ?> +
+
+ getLayout() === 'text'): ?> + task['template'], true, true) ?> + getLayout() === 'markup'): ?> + task['template']) ?> + getLayout() === 'code'): ?> +
task['template']) ?>
+ +
+
+ +
+getLayout() !== 'none'): ?> +
+ +
+ + +options['file_upload'] && $solution->folder && count($solution->folder->file_refs)): ?> + folder->file_refs as $file_ref): ?> + isImage()): ?> +
+ name) ?>: +
+
+ +
+ + + +
+ +
+ + + + + + + + + + + + + folder->file_refs as $file_ref): ?> + + + + + + + + + + folder && count($solution->folder->file_refs) > 1): ?> + + + + + + +
+ + + + + + + +
+ + asImg(['title' => _('Datei herunterladen')]) ?> + name) ?> + + + file->size / 1024) ?> + + file->user_id, 'no_title')) ?> + + file->mkdate) ?> +
+ url_for('file/download_folder', $solution->folder->id)) ?> +
+ + +task['answers'][0]['text'] != ''): ?> +
+ +
+
+ task['answers'][0]['text']) ?> +
+ diff --git a/app/views/vips/exercises/TextTask/edit.php b/app/views/vips/exercises/TextTask/edit.php new file mode 100644 index 0000000..df7304e --- /dev/null +++ b/app/views/vips/exercises/TextTask/edit.php @@ -0,0 +1,47 @@ + + + + + + + +
+ + + +
diff --git a/app/views/vips/exercises/TextTask/print.php b/app/views/vips/exercises/TextTask/print.php new file mode 100644 index 0000000..e8e29c5 --- /dev/null +++ b/app/views/vips/exercises/TextTask/print.php @@ -0,0 +1,83 @@ + +getLayout() !== 'none'): ?> + commented_solution != '') : ?> +
+ +
+ + commented_solution) ?> + id && $response[0] != '') : ?> +
+ +
+ +
+ getLayout() === 'markup'): ?> + + getLayout() === 'code'): ?> +
+ + + +
+ +
+ +
+ +
+ getLayout() === 'markup'): ?> + task['template']) ?> + getLayout() === 'code'): ?> +
task['template']) ?>
+ + task['template'], true, true) ?> + +
+ + + +options['file_upload'] && $solution && $solution->folder && count($solution->folder->file_refs)): ?> + folder->file_refs as $file_ref): ?> + isImage()): ?> +
+ name) ?>: +
+
+ +
+ + + +
+ +
+ + + + +task['answers'][0]['text'] != '') : ?> +
+ +
+ + task['answers'][0]['text']) ?> + diff --git a/app/views/vips/exercises/TextTask/solve.php b/app/views/vips/exercises/TextTask/solve.php new file mode 100644 index 0000000..ddafce6 --- /dev/null +++ b/app/views/vips/exercises/TextTask/solve.php @@ -0,0 +1,131 @@ + +getLayout() !== 'none'): ?> + task['template'] != ''): ?> +
+ + + + + +
+ getLayout() === 'markup'): ?> + + getLayout() === 'code'): ?> + + + + +
+
+ + + +options['file_upload']): ?> +
+ folder && count($solution->folder->file_refs)): ?> + + + + + (range_id)['file_size'] / 1048576) ?>) +
+ + + folder && count($solution->folder->file_refs)): ?> + + + + + + + + + + + + folder->file_refs as $file_ref): ?> + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ + + asImg(['title' => _('Datei herunterladen')]) ?> + name) ?> + + + file->size / 1024) ?> + + file->user_id, 'no_title')) ?> + + file->mkdate) ?> + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Datei löschen')]) ?> +
+ 'vips_file_upload', 'data-label' => _('%d Dateien ausgewählt')]) ?> + + +
+ diff --git a/app/views/vips/exercises/TextTask/xml.php b/app/views/vips/exercises/TextTask/xml.php new file mode 100644 index 0000000..dd79d53 --- /dev/null +++ b/app/views/vips/exercises/TextTask/xml.php @@ -0,0 +1,61 @@ + +options['comment']): ?> feedback="true"> + + <?= htmlReady($exercise->title) ?> + + + description) ?> + + options['hint'] != ''): ?> + + options['hint']) ?> + + + + + + task['template'] != ''): ?> + + task['template']) ?> + + + task['answers'] as $answer): ?> + + + + + + + task['layout'])): ?> + + + options['file_upload']): ?> + + + + task['compare'])): ?> + + + + + options['feedback'] != ''): ?> + + options['feedback']) ?> + + + + + folder): ?> + options['files_hidden']): ?> hidden="true"> + folder->file_refs as $file_ref): ?> + + + + + diff --git a/app/views/vips/exercises/correct_exercise.php b/app/views/vips/exercises/correct_exercise.php new file mode 100644 index 0000000..c62cb0e --- /dev/null +++ b/app/views/vips/exercises/correct_exercise.php @@ -0,0 +1,39 @@ + +
+ + . + title) ?> +
+ + + + + +
+
+ +
+ description) ?> +
+ + render_partial('vips/exercises/show_exercise_hint') ?> + render_partial('vips/exercises/show_exercise_files') ?> + + render_partial($exercise->getCorrectionTemplate($solution)) ?> + + options['comment']) && $solution->student_comment != '') : ?> +
+ +
+
+ student_comment, true, true) ?> +
+ +
diff --git a/app/views/vips/exercises/courseware_block.php b/app/views/vips/exercises/courseware_block.php new file mode 100644 index 0000000..4259f90 --- /dev/null +++ b/app/views/vips/exercises/courseware_block.php @@ -0,0 +1,79 @@ + + + + 0 && !$show_solution): ?> + + + +

+ title) ?> + +
+ + + + + +
+

+ +
+ description) ?> +
+ +render_partial('vips/exercises/show_exercise_hint') ?> +render_partial('vips/exercises/show_exercise_files') ?> + + + render_partial($exercise->getCorrectionTemplate($solution), ['show_solution' => $sample_solution]) ?> + + options['comment'] && $solution->student_comment != ''): ?> +
+ +
+
+ student_comment, true, true) ?> +
+ + +
+ +
+ + feedback != ''): ?> +
+ +
+
+ feedback) ?> +
+ + +
+ points, $max_points) ?> +
+ + render_partial($exercise->getSolveTemplate($solution, $assignment, $user_id)) ?> + + options['comment'])): ?> + + + + + diff --git a/app/views/vips/exercises/evaluation_mode_info.php b/app/views/vips/exercises/evaluation_mode_info.php new file mode 100644 index 0000000..d39d01e --- /dev/null +++ b/app/views/vips/exercises/evaluation_mode_info.php @@ -0,0 +1,22 @@ + +itemCount() > 1): ?> +
+ + + + + +
+ + + +
+ ', '') ?> +
+ diff --git a/app/views/vips/exercises/flexible_input.php b/app/views/vips/exercises/flexible_input.php new file mode 100644 index 0000000..f2e8940 --- /dev/null +++ b/app/views/vips/exercises/flexible_input.php @@ -0,0 +1,25 @@ + +
+ + + + + value="" + + > +
+ + +
+
+asInput(['class' => 'textarea_toggle small_input', 'title' => _('Auf mehrzeilige Eingabe umschalten')]) ?> +asInput(['class' => 'textarea_toggle large_input', 'title' => _('Auf einzeilige Eingabe umschalten')]) ?> diff --git a/app/views/vips/exercises/flexible_textarea.php b/app/views/vips/exercises/flexible_textarea.php new file mode 100644 index 0000000..64db62f --- /dev/null +++ b/app/views/vips/exercises/flexible_textarea.php @@ -0,0 +1,17 @@ + +
+ +
+ +
+ +
diff --git a/app/views/vips/exercises/print_exercise.php b/app/views/vips/exercises/print_exercise.php new file mode 100644 index 0000000..a3da7ba --- /dev/null +++ b/app/views/vips/exercises/print_exercise.php @@ -0,0 +1,64 @@ + + + +
+

+ . + title) ?> + +
+ + + + + +
+

+ +
+ description) ?> +
+ + render_partial('vips/exercises/show_exercise_hint') ?> + render_partial('vips/exercises/show_exercise_files') ?> + + render_partial($exercise->getPrintTemplate($solution, $assignment, $user_id)) ?> + + student_comment != '') : ?> +
+ +
+ + student_comment, true, true) ?> + + + + + feedback != ''): ?> +
+ +
+ + feedback) ?> + + + render_partial('vips/solutions/feedback_files') ?> + + +
+ points, $max_points) ?> +
+ +
+ + diff --git a/app/views/vips/exercises/show_exercise_files.php b/app/views/vips/exercises/show_exercise_files.php new file mode 100644 index 0000000..822fcee --- /dev/null +++ b/app/views/vips/exercises/show_exercise_files.php @@ -0,0 +1,20 @@ + +folder && count($exercise->folder->file_refs) > 0 && !$exercise->options['files_hidden']): ?> +
+ +
+ + + diff --git a/app/views/vips/exercises/show_exercise_hint.php b/app/views/vips/exercises/show_exercise_hint.php new file mode 100644 index 0000000..8d68647 --- /dev/null +++ b/app/views/vips/exercises/show_exercise_hint.php @@ -0,0 +1,12 @@ + +options['hint']) && $exercise->options['hint'] !== ''): ?> +
+

+ options['hint']) ?> +
+
+ diff --git a/app/views/vips/pool/assignments.php b/app/views/vips/pool/assignments.php new file mode 100644 index 0000000..795e2f6 --- /dev/null +++ b/app/views/vips/pool/assignments.php @@ -0,0 +1,25 @@ + + +
+
+

+ +

+ url_for('vips/sheets/edit_assignment')) ?> +
+ + render_partial('vips/pool/list_assignments') ?> + + + diff --git a/app/views/vips/pool/copy_exercises_dialog.php b/app/views/vips/pool/copy_exercises_dialog.php new file mode 100644 index 0000000..810be11 --- /dev/null +++ b/app/views/vips/pool/copy_exercises_dialog.php @@ -0,0 +1,50 @@ + +
+ + $assignment_id): ?> + + + + + +
+ +
+
diff --git a/app/views/vips/pool/exercises.php b/app/views/vips/pool/exercises.php new file mode 100644 index 0000000..de11e7a --- /dev/null +++ b/app/views/vips/pool/exercises.php @@ -0,0 +1,15 @@ + + + + + render_partial('vips/pool/list_exercises') ?> + + + diff --git a/app/views/vips/pool/list_assignments.php b/app/views/vips/pool/list_assignments.php new file mode 100644 index 0000000..4caf303 --- /dev/null +++ b/app/views/vips/pool/list_assignments.php @@ -0,0 +1,174 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + +
+ + + + getTypeIcon() ?> + + + + + + + + + + + + + + + + + name) ?> + + + + addLink( + $controller->url_for('vips/sheets/show_assignment', ['cid' => $course_id, 'assignment_id' => $assignment['id']]), + _('Studierendensicht anzeigen'), + Icon::create('community') + ) ?> + + addLink( + $controller->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment['id']]), + _('Aufgabenblatt drucken'), + Icon::create('print'), + ['target' => '_blank'] + ) ?> + + addLink( + $controller->url_for('vips/sheets/copy_assignments_dialog', ['assignment_ids[]' => $assignment['id']]), + _('Aufgabenblatt kopieren'), + Icon::create('copy'), + ['data-dialog' => 'size=auto'] + ) ?> + + isLocked()): ?> + addButton('reset', _('Alle Lösungen zurücksetzen'), Icon::create('refresh'), [ + 'formaction' => $controller->url_for('vips/sheets/reset_assignment', ['assignment_id' => $assignment['id']]), + 'data-confirm' => _('Achtung: Wenn Sie die Lösungen zurücksetzen, werden die Lösungen aller Teilnehmenden archiviert!') + ]) ?> + + addButton('delete', _('Aufgabenblatt löschen'), Icon::create('trash'), [ + 'formaction' => $controller->url_for('vips/sheets/delete_assignments', ['assignment_ids[]' => $assignment['id']]), + 'data-confirm' => sprintf(_('Wollen Sie wirklich das Aufgabenblatt „%s“ löschen?'), $assignment['test_title']) + ]) ?> + + render() ?> +
+ 'batch_action', + 'data-dialog' => 'size=auto', + 'formaction' => $controller->url_for('vips/sheets/copy_assignments_dialog') + ]) ?> + 'batch_action', + 'data-dialog' => 'size=auto', + 'formaction' => $controller->url_for('vips/sheets/move_assignments_dialog') + ]) ?> + 'batch_action', + 'formaction' => $controller->url_for('vips/sheets/delete_assignments'), + 'data-confirm' => _('Wollen Sie wirklich die ausgewählten Aufgabenblätter löschen?') + ]) ?> + + page_chooser($controller->url_for('vips/pool/assignments', ['page' => '%d', 'sort' => $sort, 'desc' => $desc, 'search_filter' => $search_filter]), $count, $page) ?> +
+
diff --git a/app/views/vips/pool/list_exercises.php b/app/views/vips/pool/list_exercises.php new file mode 100644 index 0000000..83544f0 --- /dev/null +++ b/app/views/vips/pool/list_exercises.php @@ -0,0 +1,151 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + addLink($controller->url_for('vips/sheets/show_exercise', ['cid' => $course_id, 'assignment_id' => $exercise['assignment_id'], 'exercise_id' => $exercise['id']]), + _('Studierendensicht anzeigen'), Icon::create('community') + ) ?> + + addLink($controller->url_for('vips/pool/copy_exercises_dialog', ["exercise_ids[{$exercise['id']}]" => $exercise['assignment_id']]), + _('Aufgabe kopieren'), Icon::create('copy'), ['data-dialog' => 'size=auto'] + ) ?> + + addButton('delete', _('Aufgabe löschen'), Icon::create('trash'), [ + 'formaction' => $controller->url_for('vips/pool/delete_exercises', ["exercise_ids[{$exercise['id']}]" => $exercise['assignment_id']]), + 'data-confirm' => sprintf(_('Wollen Sie wirklich die Aufgabe „%s“ löschen?'), $exercise['title']) + ]) ?> + render() ?> +
+ 'batch_action', + 'data-dialog' => 'size=auto', + 'formaction' => $controller->url_for('vips/pool/copy_exercises_dialog') + ]) ?> + 'batch_action', + 'data-dialog' => 'size=auto', + 'formaction' => $controller->url_for('vips/pool/move_exercises_dialog') + ]) ?> + 'batch_action', + 'formaction' => $controller->url_for('vips/pool/delete_exercises'), + 'data-confirm' => _('Wollen Sie wirklich die ausgewählten Aufgaben löschen?') + ]) ?> + + page_chooser($controller->url_for('vips/pool/exercises', ['page' => '%d', 'sort' => $sort, 'desc' => $desc, 'search_filter' => $search_filter]), $count, $page) ?> +
+
diff --git a/app/views/vips/pool/move_exercises_dialog.php b/app/views/vips/pool/move_exercises_dialog.php new file mode 100644 index 0000000..09e7ac3 --- /dev/null +++ b/app/views/vips/pool/move_exercises_dialog.php @@ -0,0 +1,50 @@ + +
+ + $assignment_id): ?> + + + + + +
+ +
+
diff --git a/app/views/vips/sheets/add_exercise_dialog.php b/app/views/vips/sheets/add_exercise_dialog.php new file mode 100644 index 0000000..f13a08d --- /dev/null +++ b/app/views/vips/sheets/add_exercise_dialog.php @@ -0,0 +1,26 @@ +, array> $exercise_types + */ +?> +
+ + +
+ + + + +
+ $entry): ?> + + +
+
+
diff --git a/app/views/vips/sheets/assign_block_dialog.php b/app/views/vips/sheets/assign_block_dialog.php new file mode 100644 index 0000000..ba75f96 --- /dev/null +++ b/app/views/vips/sheets/assign_block_dialog.php @@ -0,0 +1,32 @@ + +
+ + + + + + + +
+ +
+
diff --git a/app/views/vips/sheets/assignment_type_tooltip.php b/app/views/vips/sheets/assignment_type_tooltip.php new file mode 100644 index 0000000..a6bf99e --- /dev/null +++ b/app/views/vips/sheets/assignment_type_tooltip.php @@ -0,0 +1,18 @@ +
+
+
+ +
+
+
+ +
+
+
+ +
+
+ + + + diff --git a/app/views/vips/sheets/content_bar_icons.php b/app/views/vips/sheets/content_bar_icons.php new file mode 100644 index 0000000..5d6268a --- /dev/null +++ b/app/views/vips/sheets/content_bar_icons.php @@ -0,0 +1,19 @@ + + + asImg(24, ['title' => _('Vorige Aufgabe')]) ?> + + + + asImg(24) ?> + + + + + + asImg(24, ['title' => _('Nächste Aufgabe')]) ?> + + + + asImg(24) ?> + + diff --git a/app/views/vips/sheets/copy_assignment_dialog.php b/app/views/vips/sheets/copy_assignment_dialog.php new file mode 100644 index 0000000..49eecb9 --- /dev/null +++ b/app/views/vips/sheets/copy_assignment_dialog.php @@ -0,0 +1,104 @@ +
+ + + + + + + + + + + + 'size=1200x800', 'formaction' => $controller->url_for('vips/sheets/copy_assignment_dialog')]) ?> + 'size=1200x800', 'formaction' => $controller->url_for('vips/sheets/copy_assignment_dialog')]) ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + +
+ + + + + + + + name) ?> + +
+ page_chooser($controller->url_for('vips/sheets/copy_assignment_dialog', ['page' => '%d'] + compact('search_filter', 'sort', 'desc')), + $count, $page, 'data-dialog="size=1200x800"', $size) ?> +
+ + + + +
+ 'batch_action_d']) ?> +
+ diff --git a/app/views/vips/sheets/copy_assignments_dialog.php b/app/views/vips/sheets/copy_assignments_dialog.php new file mode 100644 index 0000000..2c37661 --- /dev/null +++ b/app/views/vips/sheets/copy_assignments_dialog.php @@ -0,0 +1,34 @@ + +
+ + + + + + + +
+ +
+
diff --git a/app/views/vips/sheets/copy_exercise_dialog.php b/app/views/vips/sheets/copy_exercise_dialog.php new file mode 100644 index 0000000..7eeec26 --- /dev/null +++ b/app/views/vips/sheets/copy_exercise_dialog.php @@ -0,0 +1,132 @@ + +
+ + + + + + + + + + + + + 'size=big', 'formaction' => $controller->url_for('vips/sheets/copy_exercise_dialog')]) ?> + 'size=big', 'formaction' => $controller->url_for('vips/sheets/copy_exercise_dialog')]) ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + name) ?> + +
+ page_chooser($controller->url_for('vips/sheets/copy_exercise_dialog', ['page' => '%d'] + compact('assignment_id', 'search_filter', 'sort', 'desc')), + $count, $page, 'data-dialog="size=big"', $size) ?> +
+ + + + +
+ 'batch_action_d']) ?> +
+
diff --git a/app/views/vips/sheets/copy_exercises_dialog.php b/app/views/vips/sheets/copy_exercises_dialog.php new file mode 100644 index 0000000..17f6678 --- /dev/null +++ b/app/views/vips/sheets/copy_exercises_dialog.php @@ -0,0 +1,52 @@ + +
+ + + + + + + + +
+ +
+
diff --git a/app/views/vips/sheets/edit_assignment.php b/app/views/vips/sheets/edit_assignment.php new file mode 100644 index 0000000..1d2c66e --- /dev/null +++ b/app/views/vips/sheets/edit_assignment.php @@ -0,0 +1,329 @@ + + +render() ?> + +
+ + + + +
+ + + + + locked): ?> + + + + + + + +
+ + + render_partial('vips/sheets/assignment_type_tooltip'), false, true) ?> + + + $entry) : ?> + + +
+ + + + type !== 'selftest' ? 'required' : '' ?> + + + + type !== 'exam' ? 'disabled' : '' ?> + + + +
+ > + + asImg(['class' => 'toggle-open']) ?> + asImg(['class' => 'toggle-closed']) ?> + + + +
+ range_type === 'course'): ?> + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+ +
+ +
+ + + +
+ + + + + + + + + + + options['feedback'])): ?> + options['feedback'] as $threshold => $feedback): ?> + + + + + + + + + + + + + + + + + + +
+ + + + + +
+ % + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Eintrag löschen')]) ?> +
+ % + + + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Eintrag löschen')]) ?> +
+ 'add_dynamic_row']) ?> +
+
+
+
+ +
+ + + + + exercise_refs)): ?> + + + + + + + + + + + + + render_partial('vips/sheets/list_exercises') ?> + + + + + + + + + + + +
+ + + + + + + + + +
+ + + url_for('vips/sheets/add_exercise_dialog', compact('assignment_id')), + ['data-dialog' => 'size=auto']) ?> + + exercise_refs)): ?> + 'batch_action', + 'formaction' => $controller->url_for('vips/sheets/copy_exercises_dialog'), + 'data-dialog' => 'size=auto' + ]) ?> + + 'batch_action', + 'formaction' => $controller->url_for('vips/sheets/move_exercises_dialog'), + 'data-dialog' => 'size=auto' + ]) ?> + 'batch_action', + 'formaction' => $controller->url_for('vips/sheets/delete_exercises'), + 'data-confirm' => _('Wollen Sie wirklich die ausgewählten Aufgaben löschen?') + ]) ?> + + + + exercise_refs) > 0): ?> +
+ getTotalPoints()) ?> +
+ +
+
diff --git a/app/views/vips/sheets/edit_exercise.php b/app/views/vips/sheets/edit_exercise.php new file mode 100644 index 0000000..156d789 --- /dev/null +++ b/app/views/vips/sheets/edit_exercise.php @@ -0,0 +1,161 @@ + + + + +render() ?> + +
+ + + id) : ?> + + + + + +
+ + id): ?> + . + + getTypeName()) ?> + id): ?> +
+ + + + + +
+ +
+ + + + + + + folder && count($exercise->folder->file_refs)): ?> + + + + + + + + + + + + folder->file_refs as $file_ref): ?> + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ + getContentDisposition() === 'inline' ? 'target="_blank"' : '' ?>> + asImg(['title' => _('Datei herunterladen')]) ?> + name) ?> + + + isImage()): ?> + <?= htmlReady($file_ref->name) ?> + + + file->size / 1024) ?> + + file->mkdate) ?> + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Datei löschen')]) ?> +
+ 'vips_file_upload', 'data-label' => _('%d Dateien ausgewählt')]) ?> + + range_id)['file_size'] / 1048576)) ?> + +
+ + folder && count($exercise->folder->file_refs)): ?> + + + + render_partial($exercise->getEditTemplate($assignment)) ?> + + + + +
+ + + + + type !== 'selftest') : ?> + + +
+
+ +
+ +
+
+ + diff --git a/app/views/vips/sheets/export_assignment.php b/app/views/vips/sheets/export_assignment.php new file mode 100644 index 0000000..1a24f3c --- /dev/null +++ b/app/views/vips/sheets/export_assignment.php @@ -0,0 +1,82 @@ +' ?> + +isUnlimited()): ?> + end="end) ?>" + + type === 'exam' && $assignment->options['duration']): ?> + duration="options['duration'] ?>" + + block_id): ?> + block="block->name) ?>" + + > + + <?= htmlReady($assignment->test->title) ?> + + + test->description) ?> + + options['notes'] != ''): ?> + + options['notes']) ?> + + + options['access_code'])): ?> + access-code="options['access_code']) ?>" + + options['ip_range'])): ?> + ip-ranges="options['ip_range']) ?>" + + options['resets'] === 0): ?> + resets="0" + + options['max_tries'])): ?> + tries="options['max_tries'] ?>" + + /> + + options['feedback'])): ?> + + options['feedback'] as $threshold => $feedback): ?> + + + + + + + + test->exercise_refs as $exercise_ref): ?> + render_partial($exercise_ref->exercise->getXMLTemplate($assignment), ['points' => $exercise_ref->points]) ?> + + + + + + + getPath())) ?> + + + + + diff --git a/app/views/vips/sheets/import_assignment_dialog.php b/app/views/vips/sheets/import_assignment_dialog.php new file mode 100644 index 0000000..f8a478b --- /dev/null +++ b/app/views/vips/sheets/import_assignment_dialog.php @@ -0,0 +1,21 @@ + +
+ + +

+ +

+ + + +
+ +
+
diff --git a/app/views/vips/sheets/ip_range_tooltip.php b/app/views/vips/sheets/ip_range_tooltip.php new file mode 100644 index 0000000..24a407f --- /dev/null +++ b/app/views/vips/sheets/ip_range_tooltip.php @@ -0,0 +1,26 @@ + + +
+
131.173.73.42
+
+ +
+
131.173.73 131.173.73.0/24
+
+ +
+
131.173.73-131.173.75
+
+ +
+ +
#94/E01
+
+ +
+ +
+ + + + diff --git a/app/views/vips/sheets/list_assignments.php b/app/views/vips/sheets/list_assignments.php new file mode 100644 index 0000000..29e85b3 --- /dev/null +++ b/app/views/vips/sheets/list_assignments.php @@ -0,0 +1,27 @@ + + +
+
+

+ +

+ url_for('vips/sheets/edit_assignment')) ?> +
+ + + $assignment_list): ?> + 0 || isset($assignment_list['block']->id)): ?> + render_partial('vips/sheets/list_assignments_list', ['i' => $i] + $assignment_list) ?> + + diff --git a/app/views/vips/sheets/list_assignments_list.php b/app/views/vips/sheets/list_assignments_list.php new file mode 100644 index 0000000..bc3fc1e --- /dev/null +++ b/app/views/vips/sheets/list_assignments_list.php @@ -0,0 +1,207 @@ + +
+ + + + + + + + + + + + + + + + + + + + + isRunning() && !$assignment->active ?> + + + + + + + + + + + + + + + + + + + +
+ + + id)): ?> + visible): ?> + + group_id): ?> + group->name)) ?> + + + + +
+ + addLink( + $controller->url_for('vips/admin/edit_block', ['block_id' => $block->id]), + _('Block bearbeiten'), + Icon::create('edit'), + ['data-dialog' => 'size=auto'] + ) ?> + addButton( + 'delete', + _('Block löschen'), + Icon::create('trash'), + [ + 'formaction' => $controller->url_for('vips/admin/delete_block', ['block_id' => $block->id]), + 'data-confirm' => sprintf(_('Wollen Sie wirklich den Block „%s“ löschen?'), $title) + ] + ) ?> + render() ?> +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + getTypeIcon() ?> + test->title) ?> + + + () + + + start) ?> + + isUnlimited()) : ?> + end) ?> + + + getTypeName()) ?> + + + isFinished()): ?> + + isRunning()): ?> + + + block_id): ?> + block->name) ?> + + + + isRunning()): ?> + active): ?> + addButton('go', _('Bearbeitung fortsetzen'), Icon::create('play'), [ + 'formaction' => $controller->url_for('vips/sheets/stopgo_assignment', ['assignment_id' => $assignment->id]) + ]) ?> + + addButton('stop', _('Bearbeitung anhalten'), Icon::create('pause'), [ + 'formaction' => $controller->url_for('vips/sheets/stopgo_assignment', ['assignment_id' => $assignment->id]) + ]) ?> + + isFinished()) : ?> + addLink($controller->url_for('vips/sheets/start_assignment_dialog', ['assignment_id' => $assignment->id]), + _('Aufgabenblatt starten'), Icon::create('play'), ['data-dialog' => 'size=auto'] + ) ?> + + + addLink($controller->url_for('vips/sheets/show_assignment', ['assignment_id' => $assignment->id]), + _('Studierendensicht anzeigen'), Icon::create('community') + ) ?> + addLink($controller->url_for('vips/solutions/assignment_solutions', ['assignment_id' => $assignment->id]), + _('Aufgaben korrigieren'), Icon::create('accept') + ) ?> + addLink($controller->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment->id]), + _('Aufgabenblatt drucken'), Icon::create('print'), ['target' => '_blank'] + ) ?> + addButton('copy', _('Aufgabenblatt duplizieren'), Icon::create('copy'), [ + 'formaction' => $controller->url_for('vips/sheets/copy_assignment', ['assignment_id' => $assignment->id]) + ]) ?> + isLocked()): ?> + addButton('reset', _('Alle Lösungen zurücksetzen'), Icon::create('refresh'), [ + 'formaction' => $controller->url_for('vips/sheets/reset_assignment', ['assignment_id' => $assignment->id]), + 'data-confirm' => _('Achtung: Wenn Sie die Lösungen zurücksetzen, werden die Lösungen aller Teilnehmenden archiviert!') + ]) ?> + + addButton('delete', _('Aufgabenblatt löschen'), Icon::create('trash'), [ + 'formaction' => $controller->url_for('vips/sheets/delete_assignment', ['assignment_id' => $assignment->id]), + 'data-confirm' => sprintf(_('Wollen Sie wirklich das Aufgabenblatt „%s“ löschen?'), $assignment->test->title) + ]) ?> + + render() ?> +
+ 1): ?> + 'batch_action_' . $i, + 'formaction' => $controller->url_for('vips/sheets/assign_block_dialog'), + 'data-dialog' => 'size=auto' + ]) ?> + + 'batch_action_' . $i, + 'formaction' => $controller->url_for('vips/sheets/copy_assignments_dialog'), + 'data-dialog' => 'size=auto' + ]) ?> + 'batch_action_' . $i, + 'formaction' => $controller->url_for('vips/sheets/move_assignments_dialog'), + 'data-dialog' => 'size=auto' + ]) ?> + 'batch_action_' . $i, + 'formaction' => $controller->url_for('vips/sheets/delete_assignments'), + 'data-confirm' => _('Wollen Sie wirklich die ausgewählten Aufgabenblätter löschen?') + ]) ?> +
+
diff --git a/app/views/vips/sheets/list_assignments_stud.php b/app/views/vips/sheets/list_assignments_stud.php new file mode 100644 index 0000000..6477557 --- /dev/null +++ b/app/views/vips/sheets/list_assignments_stud.php @@ -0,0 +1,117 @@ + + + + + + + $block): ?> + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ 1 || $block_id): ?> + + + + +
+ + + + + + + + + + + + + + + + + + + +
+ + getTypeIcon() ?> + test->title) ?> + + active): ?> + + () + + + + start) ?> + + isUnlimited()) : ?> + end) ?> + + + getTypeName()) ?> + + type === 'exam'): ?> + getAssignmentAttempt($user_id) ?> + + – + end < time()): ?> + + + + + isFinished($user_id)): ?> + + + countSolutions($user_id) ?> + + – + test->exercise_refs)): ?> + + + + + + + active && $assignment->type !== 'exam'): ?> + + addLink($controller->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment->id]), + _('Aufgabenblatt drucken'), Icon::create('print'), ['target' => '_blank'] + ) ?> + render() ?> + +
+ diff --git a/app/views/vips/sheets/list_exercises.php b/app/views/vips/sheets/list_exercises.php new file mode 100644 index 0000000..4591163 --- /dev/null +++ b/app/views/vips/sheets/list_exercises.php @@ -0,0 +1,67 @@ + +exercise_refs as $i => $exercise_ref): ?> + + + + + + + + + + + + + title) ?> + + + + + getTypeName()) ?> + + + + + + + + + + addLink( + $controller->url_for('vips/sheets/show_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $exercise->id]), + _('Studierendensicht anzeigen'), + Icon::create('community') + ) ?> + + + + addButton( + 'copy', + _('Aufgabe duplizieren'), + Icon::create('copy'), + ['formaction' => $controller->url_for('vips/sheets/copy_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $exercise->id])] + ) ?> + + + addButton( + 'delete', + _('Aufgabe löschen'), + Icon::create('trash'), + [ + 'formaction' => $controller->url_for('vips/sheets/delete_exercise', ['assignment_id' => $assignment_id, 'exercise_id' => $exercise->id]), + 'data-confirm' => sprintf(_('Wollen Sie wirklich die Aufgabe „%s“ löschen?'), $exercise->title) + ] + ) ?> + + render() ?> + + + diff --git a/app/views/vips/sheets/move_assignments_dialog.php b/app/views/vips/sheets/move_assignments_dialog.php new file mode 100644 index 0000000..c022fc1 --- /dev/null +++ b/app/views/vips/sheets/move_assignments_dialog.php @@ -0,0 +1,34 @@ + +
+ + + + + + + +
+ +
+
diff --git a/app/views/vips/sheets/move_exercises_dialog.php b/app/views/vips/sheets/move_exercises_dialog.php new file mode 100644 index 0000000..4901564 --- /dev/null +++ b/app/views/vips/sheets/move_exercises_dialog.php @@ -0,0 +1,52 @@ + +
+ + + + + + + + +
+ +
+
diff --git a/app/views/vips/sheets/print_assignment.php b/app/views/vips/sheets/print_assignment.php new file mode 100644 index 0000000..9e7b5e6 --- /dev/null +++ b/app/views/vips/sheets/print_assignment.php @@ -0,0 +1,106 @@ + +
+

+ test->title) ?> +

+ +
+ test->description) ?> +
+ +

+ : start) ?>
+ isUnlimited()): ?> + : end) ?> + +

+ + range_type === 'course'): ?> +

+ : course->name) ?> + course->veranstaltungsnummer): ?> + (course->veranstaltungsnummer) ?>) + +
+ : course->start_semester->name) ?>
+ : +

+ +

+ + :
+ + : ________________________________________
+ + type == 'exam'): ?> + + : + +
+ : _______________________________ + + +

+ + + getExerciseRefs($user_id) as $i => $exercise_ref): ?> + exercise ?> + + + + getSolution($user_id, $exercise->id); ?> + + + + + assignment = $assignment; ?> + + + render_partial('vips/exercises/print_exercise', [ + 'exercise' => $exercise, + 'exercise_position' => $i + 1, + 'max_points' => $exercise_ref->points, + 'solution' => $solution, + 'show_solution' => $print_sample_solution + ]) ?> + + + + + test->getTotalPoints(); ?> + getUserPoints($user_id); ?> + getUserFeedback($user_id); ?> +
+

+ + +
+ +
+

+ +
+ +
+ + +
+ +
+ + + +
+ + +
diff --git a/app/views/vips/sheets/print_assignments.php b/app/views/vips/sheets/print_assignments.php new file mode 100644 index 0000000..071e13c --- /dev/null +++ b/app/views/vips/sheets/print_assignments.php @@ -0,0 +1,43 @@ + +checkEditPermission()): ?> + + + + + render_partial('vips/sheets/print_assignment', $data) ?> + diff --git a/app/views/vips/sheets/print_layout.php b/app/views/vips/sheets/print_layout.php new file mode 100644 index 0000000..866fa16 --- /dev/null +++ b/app/views/vips/sheets/print_layout.php @@ -0,0 +1,9 @@ + + + + + diff --git a/app/views/vips/sheets/show_assignment.php b/app/views/vips/sheets/show_assignment.php new file mode 100644 index 0000000..4f75521 --- /dev/null +++ b/app/views/vips/sheets/show_assignment.php @@ -0,0 +1,179 @@ + + + +type === 'exam' && isset($assignment_attempt) && $remaining_time > 0) : ?> +
+ : min +
+ + +render() ?> + +

+ test->title) ?> +

+ +
+ test->description) ?> +
+ +isUnlimited()) : ?> + + start) ?> + + + start) ?> – + end) ?> + + +type === 'exam'): ?> +

+ + + 0): ?> + + + + +

+ +

+ +

+ + + +
+ + + +
+
+ + + +
+
+
+ + + +
+ + + +
+ + + + + + + + + + + +
+
+ + test->exercise_refs)): ?> + + + + + + + + + + + + + + getExerciseRefs($solver_id) as $exercise_ref): ?> + exercise ?> + getSolution($solver_id, $exercise->id) ?> + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + +
+ + + title) ?> + + + + mkdate) ?> + + + + user_id, 'no_title')) ?> + + + + asImg(['title' => _('ja')]) ?> + + asImg(['title' => _('nein')]) ?> + + + points) ?> +
+ test->getTotalPoints()) ?> +
+ + + + + + diff --git a/app/views/vips/sheets/show_exercise.php b/app/views/vips/sheets/show_exercise.php new file mode 100644 index 0000000..b680450 --- /dev/null +++ b/app/views/vips/sheets/show_exercise.php @@ -0,0 +1,109 @@ + + + + +type == 'exam' && !$assignment->checkEditPermission()) : ?> +
+ : min +
+ +
+ + +
+ + +render() ?> + + +
+ + render_partial('vips/exercises/correct_exercise') ?> + +
+ + title)) ?> + + + feedback != '') : ?> +
+ +
+
+ feedback) ?> +
+ + +
+ points, $max_points) ?> +
+
+
+ + +
+ + + + + + +
+ + . + title) ?> +
+ + + + + +
+
+ + 0): ?> + + + +
+ description) ?> +
+ + render_partial('vips/exercises/show_exercise_hint') ?> + render_partial('vips/exercises/show_exercise_files') ?> + + render_partial($exercise->getSolveTemplate($solution, $assignment, $solver_id)) ?> + + options['comment'])) : ?> + + +
+ +
+ itemCount() ? [] : ['disabled' => 'disabled']) ?> +
+
+ + + diff --git a/app/views/vips/sheets/show_exercise_link.php b/app/views/vips/sheets/show_exercise_link.php new file mode 100644 index 0000000..6f41a6a --- /dev/null +++ b/app/views/vips/sheets/show_exercise_link.php @@ -0,0 +1,23 @@ + + + + + + diff --git a/app/views/vips/sheets/start_assignment_dialog.php b/app/views/vips/sheets/start_assignment_dialog.php new file mode 100644 index 0000000..4dd47d9 --- /dev/null +++ b/app/views/vips/sheets/start_assignment_dialog.php @@ -0,0 +1,40 @@ + +
+ + + +
+ +
+ + + + type !== 'selftest' ? 'required' : '' ?> + + + +
+ + +
+
diff --git a/app/views/vips/solutions/assignment_solutions.php b/app/views/vips/solutions/assignment_solutions.php new file mode 100644 index 0000000..86b4e8a --- /dev/null +++ b/app/views/vips/solutions/assignment_solutions.php @@ -0,0 +1,308 @@ + + + +
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ test->title)) ?> + render_partial('vips/solutions/solution_color_tooltip'), false, true) ?> + + + + +
+ + + + asImg(['class' => 'arrow_all', 'title' => _('Aufgaben aller Teilnehmenden anzeigen')]) ?> + asImg(['class' => 'arrow_all', 'title' => _('Aufgaben aller Teilnehmenden verstecken'), 'style' => 'display: none;']) ?> + + + + + + + + + + + + + + +
+ + + + + asImg(['class' => 'solution-open', 'title' => _('Aufgaben anzeigen')]) ?> + asImg(['class' => 'solution-close', 'title' => _('Aufgaben verstecken')]) ?> + + + + + + type == 'exam' && $view === 'working') : ?> + + + +
+ : ()
+ : + 0): ?> + () + +
+ + + + +
+ +
+ + +
+ + + + + + – + + + + + % + + – + + + 0) : ?> + + + – + + + 0) : ?> + + + – + + + + type === 'exam' && $view !== 'pending') : ?> + isRunning()) : ?> + addLink($controller->url_for('vips/solutions/edit_assignment_attempt', ['assignment_id' => $assignment_id, 'solver_id' => $solver['user_id'], 'view' => $view]), + _('Abgabezeitpunkt bearbeiten'), Icon::create('edit'), ['data-dialog' => 'size=auto'] + ) ?> + addButton('reset', _('Teilnahme und Lösungen zurücksetzen'), Icon::create('refresh'), [ + 'form' => 'post_form', + 'formaction' => $controller->url_for('vips/solutions/delete_solutions', ['assignment_id' => $assignment_id, 'solver_id' => $solver['user_id'], 'view' => $view]), + 'data-confirm' => _('Achtung: Wenn Sie die Teilnahme zurücksetzen, werden alle Lösungen der teilnehmenden Person archiviert!') + ]) ?> + + addLink($controller->url_for('vips/solutions/show_assignment_log', ['assignment_id' => $assignment_id, 'solver_id' => $solver['user_id']]), + _('Abgabeprotokoll anzeigen'), Icon::create('log'), ['data-dialog' => 'size=auto'] + ) ?> + + 0): ?> + addLink($controller->url_for('vips/solutions/download_uploads', ['assignment_id' => $assignment_id, 'solver_id' => $solver['user_id']]), + _('Abgegebene Dateien herunterladen'), Icon::create('download') + ) ?> + + addLink($controller->url_for('vips/sheets/print_assignments', ['assignment_id' => $assignment_id, 'user_ids[]' => $solver['user_id'], 'print_files' => 1, 'print_correction' => !$view]), + _('Aufgabenblatt drucken'), Icon::create('print'), ['target' => '_blank'] + ) ?> + + addLink(URLHelper::getURL('dispatch.php/messages/write', ['rec_uname' => $solver['username']]), + sprintf(_('Nachricht an „%s“ schreiben'), $solver['name']), Icon::create('mail'), ['data-dialog' => ''] + ) ?> + + + addLink(URLHelper::getURL('dispatch.php/messages/write', ['rec_uname' => $receivers]), + _('Nachricht an die Gruppe schreiben'), Icon::create('mail'), ['data-dialog' => ''] + ) ?> + isFinished()) : ?> + addLink($controller->url_for('vips/solutions/edit_group_dialog', ['assignment_id' => $assignment_id, 'solver_id' => $solver['user_id'], 'view' => $view]), + _('Personen aus der Gruppe entfernen'), Icon::create('community'), ['data-dialog' => 'size=auto'] + ) ?> + + + render() ?> +
+ + + + + + + + + + + +
+ + + + + + + + + + + + . + + + +
+ + + + + + + $max_points || $points < 0) : ?> + + () + + + + () + + + + + () + + +
+
+ 'batch_action', + 'formaction' => $controller->url_for('vips/sheets/print_assignments', ['print_files' => 1, 'print_correction' => !$view]), + 'formmethod' => 'post', + 'formtarget' => '_blank' + ]) ?> + 'batch_action', + 'formaction' => $controller->url_for('vips/solutions/write_message'), + 'formmethod' => 'post', + 'data-dialog' => '' + ]) ?> +
+
+ + diff --git a/app/views/vips/solutions/assignments.php b/app/views/vips/solutions/assignments.php new file mode 100644 index 0000000..d1629cf --- /dev/null +++ b/app/views/vips/solutions/assignments.php @@ -0,0 +1,18 @@ + + + + render_partial('vips/solutions/assignments_list', $test_data) ?> + + render_partial('vips/solutions/assignments_list_student', $test_data) ?> + + render_partial('vips/solutions/student_grade', $overview_data) ?> + + + + + diff --git a/app/views/vips/solutions/assignments_list.php b/app/views/vips/solutions/assignments_list.php new file mode 100644 index 0000000..b9865d1 --- /dev/null +++ b/app/views/vips/solutions/assignments_list.php @@ -0,0 +1,190 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + + + id]) || $block->weight !== null): ?> + + 1): ?> + + + + + + + + id])): ?> + id] as $ass): ?> + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
+ + name) ?> + visible): ?> + + group_id): ?> + group->name)) ?> + id): ?> + + + + + weight !== null): ?> + % + + +
+ + + + getTypeIcon() ?> + test->title) ?> + + + start) ?> + + isUnlimited()): ?> + end) ?> + + + + – + + asImg(['title' => _('ja')]) ?> + + asImg(['title' => _('nein')]) ?> + + + + + + + + + + + + – + + + + + type !== 'selftest' && $block->weight === null): ?> + % + + + + addLink( + $controller->url_for('vips/solutions/update_released_dialog', ['assignment_ids[]' => $ass['assignment']->id]), + _('Freigabe ändern'), + Icon::create('lock-locked'), + ['data-dialog' => 'size=auto'] + ) ?> + addLink( + $controller->url_for('vips/sheets/edit_assignment', ['assignment_id' => $ass['assignment']->id]), + _('Aufgabenblatt bearbeiten'), + Icon::create('edit') + ) ?> + addLink( + $controller->url_for('vips/sheets/print_assignments', ['assignment_id' => $ass['assignment']->id]), + _('Aufgabenblatt drucken'), + Icon::create('print'), + ['target' => '_blank'] + ) ?> + render() ?> +
+ 'batch_action', + 'formaction' => $controller->update_released_dialogURL(), + 'data-dialog' => 'size=auto' + ]) ?> + + + + +
+
+ + diff --git a/app/views/vips/solutions/assignments_list_student.php b/app/views/vips/solutions/assignments_list_student.php new file mode 100644 index 0000000..a19a361 --- /dev/null +++ b/app/views/vips/solutions/assignments_list_student.php @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + id])): ?> + + 1): ?> + + + + + + id] as $ass): ?> + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + +
+ + name) ?> + +
+ + getTypeIcon() ?> + test->title) ?> + + + start) ?> + + isUnlimited()) : ?> + end) ?> + + + + + / + + + + + + + – + + + = VipsAssignment::RELEASE_STATUS_CORRECTIONS): ?> + + addLink( + $controller->url_for('vips/sheets/print_assignments', ['assignment_id' => $ass['assignment']->id]), + _('Aufgabenblatt drucken'), + Icon::create('print'), + ['target' => '_blank'] + ) ?> + render() ?> + +
+ + + / + + + + + + + – + + +
+ + diff --git a/app/views/vips/solutions/autocorrect_dialog.php b/app/views/vips/solutions/autocorrect_dialog.php new file mode 100644 index 0000000..29392e9 --- /dev/null +++ b/app/views/vips/solutions/autocorrect_dialog.php @@ -0,0 +1,29 @@ + +
+ + + + + + +

+ +

+ + + +
+ +
+
diff --git a/app/views/vips/solutions/edit_assignment_attempt.php b/app/views/vips/solutions/edit_assignment_attempt.php new file mode 100644 index 0000000..55f13ec --- /dev/null +++ b/app/views/vips/solutions/edit_assignment_attempt.php @@ -0,0 +1,34 @@ + +
+ + + + + + + + + + + +
+ +
+
diff --git a/app/views/vips/solutions/edit_group_dialog.php b/app/views/vips/solutions/edit_group_dialog.php new file mode 100644 index 0000000..378aa73 --- /dev/null +++ b/app/views/vips/solutions/edit_group_dialog.php @@ -0,0 +1,30 @@ + +
+ + + + + +
+ +
+ + + + + +
+ +
+
diff --git a/app/views/vips/solutions/edit_solution.php b/app/views/vips/solutions/edit_solution.php new file mode 100644 index 0000000..955fd40 --- /dev/null +++ b/app/views/vips/solutions/edit_solution.php @@ -0,0 +1,216 @@ + + + + + + +
+ + + + + + + + + 'display: none;']) ?> + + render_partial('vips/exercises/correct_exercise') ?> + +
+ + +
+ grader_id)): ?> + + state): ?> + + id): ?> + + + + +
+
+ + isArchived()): ?> + feedback) : ?> +
+ +
+
+ feedback) ?> +
+ + + render_partial('vips/solutions/feedback_files_table') ?> + +
+ points, $max_points) ?> +
+ + + + + feedback_folder && count($solution->feedback_folder->file_refs)): ?> + + + + + + + + + + + + feedback_folder->file_refs as $file_ref): ?> + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ + + asImg(['title' => _('Datei herunterladen')]) ?> + name) ?> + + + file->size) ?> + + file->user_id, 'no_title')) ?> + + file->mkdate) ?> + + asInput(['class' => 'delete_dynamic_row', 'title' => _('Datei löschen')]) ?> +
+ 'vips_file_upload', 'data-label' => _('%d Dateien ausgewählt')]) ?> + + range_id)['file_size'] / 1048576)) ?> + +
+ + feedback != '' && !Studip\Markup::editorEnabled()): ?> +
+ +
+
+ feedback) ?> +
+ + + + +
+ +
+ isArchived()): ?> + $controller->url_for('vips/solutions/restore_solution')]) ?> + + + + + +
+
+ + diff --git a/app/views/vips/solutions/feedback_files.php b/app/views/vips/solutions/feedback_files.php new file mode 100644 index 0000000..4630bb5 --- /dev/null +++ b/app/views/vips/solutions/feedback_files.php @@ -0,0 +1,20 @@ + +feedback_folder && count($solution->feedback_folder->file_refs) > 0): ?> +
+ +
+ +
    + feedback_folder->file_refs as $file_ref): ?> +
  • + + name) ?> + +
  • + +
+ diff --git a/app/views/vips/solutions/feedback_files_table.php b/app/views/vips/solutions/feedback_files_table.php new file mode 100644 index 0000000..dff9869 --- /dev/null +++ b/app/views/vips/solutions/feedback_files_table.php @@ -0,0 +1,51 @@ + +feedback_folder && count($solution->feedback_folder->file_refs)): ?> +
+ +
+ + + + + + + + + + + + + feedback_folder->file_refs as $file_ref): ?> + + + + + + + + +
+ + + + + + + +
+ + asImg(['title' => _('Datei herunterladen')]) ?> + name) ?> + + + file->size) ?> + + file->user_id, 'no_title')) ?> + + file->mkdate) ?> +
+ diff --git a/app/views/vips/solutions/gradebook_dialog.php b/app/views/vips/solutions/gradebook_dialog.php new file mode 100644 index 0000000..9ebc684 --- /dev/null +++ b/app/views/vips/solutions/gradebook_dialog.php @@ -0,0 +1,39 @@ + +
+ + + + + + + + + + + + +
+ +
+
diff --git a/app/views/vips/solutions/participants_overview.php b/app/views/vips/solutions/participants_overview.php new file mode 100644 index 0000000..72bd316 --- /dev/null +++ b/app/views/vips/solutions/participants_overview.php @@ -0,0 +1,225 @@ + + + + + + + + + + 0) : ?> + + + 1) : ?> + + + + 0) : ?> + + + 1) : ?> + + + + 0) : ?> + + + 1) : ?> + + + + + + + + + + + + + + 0) : ?> + + + + 0) : ?> + + + + 0) : ?> + + + + + + + + + + + + + $list) : ?> + + + + + + + + + + + + + overall['weighting']): ?> + + + + $list) : ?> + + + + + + + + + + + + + + + + + + + + + $list) : ?> + + + + + + + + + + + + + +
+ + + + +
+ + + + +
+
+ + + +
+ + + + + +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + 100 % + +
+ + + + + + + – + + + + + + – + + + + + + + + – + + + + + + – + + + + +
+ + diff --git a/app/views/vips/solutions/show_assignment_log.php b/app/views/vips/solutions/show_assignment_log.php new file mode 100644 index 0000000..0b1b5c8 --- /dev/null +++ b/app/views/vips/solutions/show_assignment_log.php @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ nachname, $user->vorname, $user->username) ?> +
+ + + + + + + + + + +
+ + + + + + + + gethostbyaddr($log['ip_address'])) ?> + + + +
diff --git a/app/views/vips/solutions/solution_color_tooltip.php b/app/views/vips/solutions/solution_color_tooltip.php new file mode 100644 index 0000000..41c4337 --- /dev/null +++ b/app/views/vips/solutions/solution_color_tooltip.php @@ -0,0 +1,4 @@ +', '
') ?>
+', '') ?>
+', '') ?>
+', '') ?>
diff --git a/app/views/vips/solutions/statistics.php b/app/views/vips/solutions/statistics.php new file mode 100644 index 0000000..d0327b3 --- /dev/null +++ b/app/views/vips/solutions/statistics.php @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1): ?> + $item): ?> + + + + + + + + + + + + +
+ +
+ + + + + + + +
+ + getTypeIcon() ?> + test->title) ?> + + + + + + +
+ + . + + + + + + + +
+ + + + + + + +
+ + + diff --git a/app/views/vips/solutions/student_assignment_solutions.php b/app/views/vips/solutions/student_assignment_solutions.php new file mode 100644 index 0000000..5a4238b --- /dev/null +++ b/app/views/vips/solutions/student_assignment_solutions.php @@ -0,0 +1,125 @@ + + + +

+ test->title) ?> +

+ +
+ test->description) ?> +
+ + + + + + + + + + + + + + + + + + + getExerciseRefs($user_id) as $exercise_ref) : ?> + getSolution($user_id, $exercise_ref->task_id); ?> + + + + + + + + + + hasFeedback()): ?> + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + +
+ + + + getUserPoints($user_id)) ?> + + test->getTotalPoints()) ?> +
+ += VipsAssignment::RELEASE_STATUS_COMMENTS && $feedback != ''): ?> +
+

+ +

+ + +
+ + + diff --git a/app/views/vips/solutions/student_grade.php b/app/views/vips/solutions/student_grade.php new file mode 100644 index 0000000..421434f --- /dev/null +++ b/app/views/vips/solutions/student_grade.php @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + isVisible($user_id) && $item['weighting']) : ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+ + + + + + – + + + / + + + + + + + – + + + +
+ + + +
+ + + + () + +
+ + diff --git a/app/views/vips/solutions/update_released_dialog.php b/app/views/vips/solutions/update_released_dialog.php new file mode 100644 index 0000000..a8a88ad --- /dev/null +++ b/app/views/vips/solutions/update_released_dialog.php @@ -0,0 +1,42 @@ + +
+ + + + + + + + + + + + + + + +
+ +
+
diff --git a/app/views/vips/solutions/view_solution.php b/app/views/vips/solutions/view_solution.php new file mode 100644 index 0000000..bac4a89 --- /dev/null +++ b/app/views/vips/solutions/view_solution.php @@ -0,0 +1,77 @@ + + + + + +
+ render_partial('vips/exercises/correct_exercise') ?> + +
+ + title)) ?> +
+ state): ?> + + id): ?> + + + + +
+
+ + feedback != '') : ?> +
+ + + grader_id) && $assignment->type === 'practice') : ?> + grader_id); ?> + () + +
+ +
+ feedback) ?> +
+ + + render_partial('vips/solutions/feedback_files_table') ?> + +
+ points, $max_points) ?> +
+
+
+ + diff --git a/composer.json b/composer.json index 410e14d..ea95764 100644 --- a/composer.json +++ b/composer.json @@ -51,6 +51,7 @@ "lib/models/", "lib/models/calendar/", "lib/models/resources/", + "lib/models/vips/", "lib/modules/", "lib/navigation/", "lib/plugins/core/", @@ -100,6 +101,7 @@ "ext-mbstring": "*", "ext-dom": "*", "ext-iconv": "*", + "ext-simplexml": "*", "opis/json-schema": "2.3.0", "slim/slim": "4.13.0", "php-di/php-di": "7.0.0", diff --git a/db/migrations/6.0.40_add_vips_module.php b/db/migrations/6.0.40_add_vips_module.php new file mode 100644 index 0000000..8fc50c8 --- /dev/null +++ b/db/migrations/6.0.40_add_vips_module.php @@ -0,0 +1,485 @@ +exec($sql); + $id = $db->lastInsertId(); + + $sql = "INSERT INTO roles_plugins (roleid, pluginid) + SELECT roleid, ? FROM roles WHERE `system` = 'y'"; + $db->execute($sql, [$id]); + + // copy tool activations from Vips plugin + $sql = "INSERT INTO tools_activated + SELECT range_id, range_type, ?, position, metadata, mkdate, chdate FROM tools_activated + WHERE plugin_id = (SELECT pluginid FROM plugins WHERE pluginname = 'Vips')"; + $db->execute($sql, [$id]); + + // update etask tables + $sql = "ALTER TABLE etask_assignments + CHANGE type type varchar(64) COLLATE latin1_bin NOT NULL, + CHANGE active active tinyint UNSIGNED NOT NULL DEFAULT 1, + ADD weight float NOT NULL DEFAULT 0 AFTER active, + ADD block_id int DEFAULT NULL AFTER weight, + ADD KEY test_id (test_id), + ADD KEY range_id (range_id)"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_assignment_attempts + ADD ip_address varchar(39) COLLATE latin1_bin NOT NULL AFTER end, + CHANGE options options text DEFAULT NULL, + ADD UNIQUE KEY assignment_id (assignment_id,user_id)"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_responses + CHANGE response response mediumtext NOT NULL, + ADD student_comment text DEFAULT NULL AFTER response, + ADD ip_address varchar(39) COLLATE latin1_bin NOT NULL AFTER student_comment, + ADD commented_solution text DEFAULT NULL AFTER feedback, + ADD KEY assignment_id (assignment_id,task_id,user_id), + ADD KEY user_id (user_id), + ADD KEY task_id (task_id)"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_tasks + CHANGE type type varchar(64) COLLATE latin1_bin NOT NULL, + CHANGE description description mediumtext NOT NULL, + CHANGE task task mediumtext NOT NULL, + ADD KEY user_id (user_id)"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_tests + CHANGE description description mediumtext NOT NULL, + CHANGE options options text DEFAULT NULL, + ADD KEY user_id (user_id)"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_test_tasks + ADD part int NOT NULL DEFAULT 0 AFTER position, + ADD KEY task_id (task_id)"; + $db->exec($sql); + + // add new tables + $sql = "CREATE TABLE etask_blocks ( + id int NOT NULL AUTO_INCREMENT, + name varchar(255) NOT NULL, + range_id char(32) COLLATE latin1_bin NOT NULL, + group_id char(32) COLLATE latin1_bin DEFAULT NULL, + visible tinyint NOT NULL DEFAULT 1, + weight float DEFAULT NULL, + PRIMARY KEY (id), + KEY range_id (range_id) + )"; + $db->exec($sql); + + $sql = "CREATE TABLE etask_group_members ( + group_id char(32) COLLATE latin1_bin NOT NULL, + user_id char(32) COLLATE latin1_bin NOT NULL, + start int unsigned NOT NULL, + end int unsigned DEFAULT NULL, + PRIMARY KEY (group_id,user_id,start), + KEY user_id (user_id) + )"; + $db->exec($sql); + + // add settings (unless already present) + $sql = 'INSERT IGNORE INTO `config` (`field`, `value`, `type`, `range`, `mkdate`, `chdate`, `description`) + VALUES (:name, :value, :type, :range, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :description)'; + $statement = DBManager::get()->prepare($sql); + $statement->execute([ + ':name' => 'VIPS_COURSE_GRADES', + ':description' => 'Kursbezogenes Schema zur Notenverteilung in Vips', + ':range' => 'course', + ':type' => 'array', + ':value' => '[]' + ]); + $statement->execute([ + ':name' => 'VIPS_EXAM_RESTRICTIONS', + ':description' => 'Sperrt während einer Klausur andere Bereiche von Stud.IP für die Teilnehmenden', + ':range' => 'global', + ':type' => 'boolean', + ':value' => '0' + ]); + $statement->execute([ + ':name' => 'VIPS_EXAM_ROOMS', + ':description' => 'Zentral verwaltete IP-Adressen für PC-Räume', + ':range' => 'global', + ':type' => 'array', + ':value' => '[]' + ]); + $statement->execute([ + ':name' => 'VIPS_EXAM_TERMS', + ':description' => 'Teilnahmebedingungen, die vor Beginn einer Klausur zu akzeptieren sind', + ':range' => 'global', + ':type' => 'string', + ':value' => '' + ]); + + // copy data from Vips plugin + $result = $db->query("SHOW TABLES LIKE 'vips_assignment'"); + + if ($result->rowCount() > 0) { + $this->copyVipsData(); + } + } + + private function copyVipsData() + { + $db = DBManager::get(); + $now = time(); + + $task_id = []; + $test_id = []; + $assignment_id = []; + $response_id = []; + $group_id = []; + $folder_id = []; + + $task_mapping = [ + 'sc_exercise' => 'SingleChoiceTask', + 'mc_exercise' => 'MultipleChoiceTask', + 'mco_exercise' => 'MatrixChoiceTask', + 'lt_exercise' => 'TextLineTask', + 'tb_exercise' => 'TextTask', + 'cloze_exercise' => 'ClozeTask', + 'rh_exercise' => 'MatchingTask', + 'seq_exercise' => 'SequenceTask' + ]; + + // etask_tasks + $sql = 'INSERT INTO etask_tasks (type, title, description, task, user_id, mkdate, chdate, options) + VALUES (:type, :title, :description, :task, :user_id, :mkdate, :chdate, :options)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_exercise'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + $values = [ + 'type' => $task_mapping[$row['type']] ?? $row['type'], + 'title' => $row['title'], + 'description' => $row['description'], + 'task' => $row['task_json'], + 'user_id' => $row['user_id'], + 'mkdate' => strtotime($row['created']), + 'chdate' => $now, + 'options' => $row['options'] ?: '[]' + ]; + $stmt->execute($values); + $task_id[$row['id']] = $db->lastInsertId(); + } + + // etask_tests + $sql = 'INSERT INTO etask_tests (title, description, user_id, mkdate, chdate, options) + VALUES (:title, :description, :user_id, :mkdate, :chdate, :options)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_test'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + $values = [ + 'title' => $row['title'], + 'description' => $row['description'], + 'user_id' => $row['user_id'], + 'mkdate' => strtotime($row['created']), + 'chdate' => $now, + 'options' => null + ]; + $stmt->execute($values); + $test_id[$row['id']] = $db->lastInsertId(); + } + + // etask_test_tasks + $sql = 'INSERT INTO etask_test_tasks (test_id, task_id, position, part, points, options, mkdate, chdate) + VALUES (:test_id, :task_id, :position, :part, :points, :options, :mkdate, :chdate)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_exercise_ref'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + if (isset($test_id[$row['test_id']]) && isset($task_id[$row['exercise_id']])) { + $values = [ + 'test_id' => $test_id[$row['test_id']], + 'task_id' => $task_id[$row['exercise_id']], + 'position' => $row['position'], + 'part' => $row['part'], + 'points' => $row['points'], + 'mkdate' => $now, + 'chdate' => $now, + 'options' => '', + ]; + $stmt->execute($values); + } + } + + // etask_assignments + $sql = 'INSERT INTO etask_assignments (test_id, range_type, range_id, type, start, end, active, weight, block_id, options, mkdate, chdate) + VALUES (:test_id, :range_type, :range_id, :type, :start, :end, :active, :weight, :block_id, :options, :mkdate, :chdate)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_assignment'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + if (isset($test_id[$row['test_id']])) { + $options = json_decode($row['options'], true); + unset($options['shuffle_answers']); + unset($options['printable']); + + $values = [ + 'test_id' => $test_id[$row['test_id']], + 'range_type' => $row['context'], + 'range_id' => $row['course_id'], + 'type' => $row['type'], + 'start' => strtotime($row['start']), + 'end' => strtotime($row['end']), + 'active' => $row['active'], + 'weight' => $row['weight'], + 'block_id' => $row['block_id'], + 'options' => json_encode($options), + 'mkdate' => $now, + 'chdate' => $now + ]; + $stmt->execute($values); + $assignment_id[$row['id']] = $db->lastInsertId(); + } + } + + // etask_assignment_attempts + $sql = 'INSERT INTO etask_assignment_attempts (assignment_id, user_id, start, end, ip_address, options, mkdate, chdate) + VALUES (:assignment_id, :user_id, :start, :end, :ip_address, :options, :mkdate, :chdate)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_assignment_attempt'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + if (isset($assignment_id[$row['assignment_id']])) { + $values = [ + 'assignment_id' => $assignment_id[$row['assignment_id']], + 'user_id' => $row['user_id'], + 'start' => strtotime($row['start']), + 'end' => $row['end'] ? strtotime($row['end']) : null, + 'ip_address' => $row['ip_address'], + 'options' => $row['options'], + 'mkdate' => $now, + 'chdate' => $now + ]; + $stmt->execute($values); + } + } + + // etask_responses + $sql = 'INSERT INTO etask_responses (assignment_id, task_id, user_id, response, student_comment, ip_address, state, points, feedback, commented_solution, grader_id, mkdate, chdate, options) + SELECT :assignment_id, :task_id, user_id, response, student_comment, ip_address, corrected, points, corrector_comment, commented_solution, corrector_id, UNIX_TIMESTAMP(time), UNIX_TIMESTAMP(correction_time), options + FROM :table WHERE id = :id'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT id, exercise_id, assignment_id, 0 as archive FROM vips_solution UNION SELECT id, exercise_id, assignment_id, 1 as archive FROM vips_solution_archive ORDER BY id'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + if (isset($assignment_id[$row['assignment_id']]) && isset($task_id[$row['exercise_id']])) { + $stmt->bindValue(':assignment_id', $assignment_id[$row['assignment_id']]); + $stmt->bindValue(':task_id', $task_id[$row['exercise_id']]); + $stmt->bindValue(':table', $row['archive'] ? 'vips_solution_archive' : 'vips_solution', StudipPDO::PARAM_COLUMN); + $stmt->bindValue(':id', $row['id']); + $stmt->execute(); + $response_id[$row['id']] = $db->lastInsertId(); + } + } + + // statusgruppen + $sql = 'INSERT INTO statusgruppen (statusgruppe_id, name, range_id, position, size, mkdate, chdate) + VALUES (:statusgruppe_id, :name, :range_id, :position, :size, :mkdate, :chdate)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_group'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + $id = md5($row['id'] . ':' . uniqid('statusgruppen', true)); + $position = $db->fetchColumn('SELECT MAX(position) FROM statusgruppen WHERE range_id = ?', [$row['course_id']]); + + $values = [ + 'statusgruppe_id' => $id, + 'name' => $row['name'], + 'range_id' => $row['course_id'], + 'position' => $position + 1, + 'size' => $row['size'], + 'mkdate' => $now, + 'chdate' => $now + ]; + $stmt->execute($values); + $group_id[$row['id']] = $id; + } + + // etask_blocks + $sql = 'INSERT INTO etask_blocks (id, name, range_id, group_id, visible, weight) + SELECT id, name, course_id, group_id, visible, weight FROM vips_block'; + $db->exec($sql); + + // etask_group_members + $sql = 'INSERT INTO etask_group_members (group_id, user_id, start, end) + VALUES (:group_id, :user_id, :start, :end)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_group_member'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + if (isset($group_id[$row['group_id']])) { + $values = [ + 'group_id' => $group_id[$row['group_id']], + 'user_id' => $row['user_id'], + 'start' => strtotime($row['start']), + 'end' => strtotime($row['end']) + ]; + $stmt->execute($values); + } + } + + // files + $sql = 'INSERT INTO files (id, user_id, mime_type, name, size, mkdate, chdate) + VALUES (:id, :user_id, :mime_type, :name, :size, :mkdate, :chdate)'; + $stmt = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_file'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + $values = [ + 'id' => $row['id'], + 'user_id' => $row['user_id'], + 'mime_type' => $row['mime_type'], + 'name' => $row['name'], + 'size' => $row['size'], + 'mkdate' => strtotime($row['created']), + 'chdate' => $now + ]; + $stmt->execute($values); + } + + // folders and file_refs + $sql = 'INSERT INTO folders (id, user_id, parent_id, range_id, range_type, folder_type, name, data_content, description, mkdate, chdate) + VALUES (:id, :user_id, :parent_id, :range_id, :range_type, :folder_type, :name, :data_content, :description, :mkdate, :chdate)'; + $stmt_folder = $db->prepare($sql); + $sql = "INSERT INTO file_refs (id, file_id, folder_id, description, content_terms_of_use_id, user_id, name, mkdate, chdate) + VALUES (:id, :file_id, :folder_id, :description, 'UNDEF_LICENSE', :user_id, :name, :mkdate, :chdate)"; + $stmt_file_ref = $db->prepare($sql); + $data = $db->query('SELECT * FROM vips_file_ref JOIN vips_file ON vips_file_ref.file_id = vips_file.id'); + + while ($row = $data->fetch(PDO::FETCH_ASSOC)) { + if ($row['type'] === 'exercise') { + $range_id = $task_id[$row['object_id']] ?? null; + $range_type = 'task'; + $folder_type = 'ExerciseFolder'; + } else { + $range_id = $response_id[$row['object_id']] ?? null; + $range_type = 'response'; + $folder_type = $row['type'] === 'solution' ? 'ResponseFolder' : 'FeedbackFolder'; + } + + if (isset($range_id)) { + if (!isset($folder_id[$row['object_id'] . ':' . $row['type']])) { + $new_folder_id = md5($row['object_id'] . ':' . uniqid('folders', true)); + $values = [ + 'id' => $new_folder_id, + 'user_id' => $row['user_id'], + 'parent_id' => '', + 'range_id' => $range_id, + 'range_type' => $range_type, + 'folder_type' => $folder_type, + 'name' => '', + 'data_content' => '', + 'description' => '', + 'mkdate' => strtotime($row['created']), + 'chdate' => $now + ]; + $stmt_folder->execute($values); + $folder_id[$row['object_id'] . ':' . $row['type']] = $new_folder_id; + } + + $file_ref_id = md5($row['file_id'] . ':' . $row['object_id'] . ':' . uniqid('file_refs' , true)); + $values = [ + 'id' => $file_ref_id, + 'file_id' => $row['file_id'], + 'folder_id' => $folder_id[$row['object_id'] . ':' . $row['type']], + 'description' => '', + 'user_id' => $row['user_id'], + 'name' => $row['name'], + 'mkdate' => strtotime($row['created']), + 'chdate' => $now + ]; + $stmt_file_ref->execute($values); + } + } + } + + public function down() + { + $db = DBManager::get(); + + // unregister core plugin + $sql = "DELETE plugins, roles_plugins, tools_activated FROM plugins + LEFT JOIN roles_plugins USING (pluginid) + LEFT JOIN tools_activated ON plugin_id = pluginid + WHERE pluginclassname = 'VipsModule'"; + $db->exec($sql); + + // update etask tables + $sql = "ALTER TABLE etask_assignments + CHANGE type type varchar(64) NOT NULL, + CHANGE active active tinyint UNSIGNED NOT NULL, + DROP weight, + DROP block_id, + DROP KEY test_id, + DROP KEY range_id"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_assignment_attempts + DROP ip_address, + CHANGE options options text NOT NULL, + DROP KEY assignment_id"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_responses + CHANGE response response text NOT NULL, + DROP student_comment, + DROP ip_address, + DROP commented_solution, + DROP KEY assignment_id, + DROP KEY user_id, + DROP KEY task_id"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_tasks + CHANGE type type varchar(64) NOT NULL, + CHANGE description description text NOT NULL, + CHANGE task task text NOT NULL, + DROP KEY user_id"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_tests + CHANGE description description text NOT NULL, + CHANGE options options text NOT NULL, + DROP KEY user_id"; + $db->exec($sql); + + $sql = "ALTER TABLE etask_test_tasks + DROP part, + DROP KEY task_id"; + $db->exec($sql); + + // drop new tables + $db->exec('DROP TABLE etask_blocks, etask_group_members'); + + // remove config entries + $sql = "DELETE config, config_values + FROM config + LEFT JOIN config_values USING (field) + WHERE field IN ( + 'VIPS_COURSE_GRADES', + 'VIPS_EXAM_RESTRICTIONS', + 'VIPS_EXAM_ROOMS', + 'VIPS_EXAM_TERMS' + )"; + $db->exec($sql); + } +} diff --git a/lib/classes/SimpleORMap.php b/lib/classes/SimpleORMap.php index d8cdb8e..bea9595 100644 --- a/lib/classes/SimpleORMap.php +++ b/lib/classes/SimpleORMap.php @@ -536,7 +536,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate /** * build object with given data * - * @param array $data assoc array of record + * @param iterable $data assoc array of record * @param ?bool $is_new set object to new state * @return static */ @@ -551,7 +551,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate /** * build object with given data and mark it as existing * - * @param array $data assoc array of record + * @param iterable $data assoc array of record * @return static */ public static function buildExisting($data) diff --git a/lib/classes/sidebar/VipsSearchWidget.php b/lib/classes/sidebar/VipsSearchWidget.php new file mode 100644 index 0000000..dbfdea6 --- /dev/null +++ b/lib/classes/sidebar/VipsSearchWidget.php @@ -0,0 +1,42 @@ +needles as $needle) { + if ($needle['quick_search']) { + $quick_search = QuickSearch::get($needle['name'], $needle['quick_search']); + $quick_search->noSelectbox(); + if (isset($needle['value'])) { + $quick_search->defaultValue(null, $needle['value']); + } + if (isset($needle['js_func'])) { + $quick_search->fireJSFunctionOnSelect($needle['js_func']); + } + + $needle['quick_search'] = $quick_search; + $needles[] = $needle; + } + } + + return parent::render($variables + compact('needles')); + } +} diff --git a/lib/filesystem/ExerciseFolder.php b/lib/filesystem/ExerciseFolder.php new file mode 100644 index 0000000..e400bbc --- /dev/null +++ b/lib/filesystem/ExerciseFolder.php @@ -0,0 +1,111 @@ +range_id); + + foreach ($exercise->tests as $test) { + foreach ($test->assignments as $assignment) { + if ($assignment->checkEditPermission($user_id) || + $assignment->checkViewPermission($user_id) && + ($assignment->checkAccess($user_id) || $assignment->releaseStatus($user_id) >= 3)) { + return true; + } + } + } + + return false; + } + + /** + * @param string $user_id + * @return bool + */ + public function isWritable($user_id) + { + $exercise = Exercise::find($this->range_id); + + foreach ($exercise->tests as $test) { + foreach ($test->assignments as $assignment) { + if ($assignment->checkEditPermission($user_id)) { + return true; + } + } + } + + return false; + } + + /** + * @param string $user_id + * @return bool + */ + public function isEditable($user_id) + { + return false; + } + + /** + * @param string $user_id + * @return bool + */ + public function isSubfolderAllowed($user_id) + { + return false; + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileDownloadable($fileref_or_id, $user_id) + { + return $this->isReadable($user_id); + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileEditable($fileref_or_id, $user_id) + { + return $this->isWritable($user_id); + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileWritable($fileref_or_id, $user_id) + { + return $this->isWritable($user_id); + } +} diff --git a/lib/filesystem/FeedbackFolder.php b/lib/filesystem/FeedbackFolder.php new file mode 100644 index 0000000..17511b8 --- /dev/null +++ b/lib/filesystem/FeedbackFolder.php @@ -0,0 +1,96 @@ +range_id); + $assignment = $solution->assignment; + + return $assignment->checkEditPermission() || + $assignment->checkViewPermission() && $assignment->releaseStatus($user_id) >= 2; + } + + /** + * @param string $user_id + * @return bool + */ + public function isWritable($user_id) + { + $solution = VipsSolution::find($this->range_id); + $assignment = $solution->assignment; + + return $assignment->checkEditPermission(); + } + + /** + * @param string $user_id + * @return bool + */ + public function isEditable($user_id) + { + return false; + } + + /** + * @param string $user_id + * @return bool + */ + public function isSubfolderAllowed($user_id) + { + return false; + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileDownloadable($fileref_or_id, $user_id) + { + return $this->isReadable($user_id); + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileEditable($fileref_or_id, $user_id) + { + return $this->isWritable($user_id); + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileWritable($fileref_or_id, $user_id) + { + return $this->isWritable($user_id); + } +} diff --git a/lib/filesystem/ResponseFolder.php b/lib/filesystem/ResponseFolder.php new file mode 100644 index 0000000..598bf28 --- /dev/null +++ b/lib/filesystem/ResponseFolder.php @@ -0,0 +1,107 @@ +range_id); + $assignment = $solution->assignment; + + if (!$assignment->checkViewPermission()) { + return false; + } + + if ($assignment->checkEditPermission() || $solution->user_id === $user_id) { + return true; + } + + $group = $assignment->getUserGroup($solution->user_id); + $group2 = $assignment->getUserGroup($user_id); + + return isset($group, $group2) + && $group->id === $group2->id; + } + + /** + * @param string $user_id + * @return bool + */ + public function isWritable($user_id) + { + $solution = VipsSolution::find($this->range_id); + $assignment = $solution->assignment; + + return $assignment->checkEditPermission(); + } + + /** + * @param string $user_id + * @return bool + */ + public function isEditable($user_id) + { + return false; + } + + /** + * @param string $user_id + * @return bool + */ + public function isSubfolderAllowed($user_id) + { + return false; + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileDownloadable($fileref_or_id, $user_id) + { + return $this->isReadable($user_id); + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileEditable($fileref_or_id, $user_id) + { + return $this->isWritable($user_id); + } + + /** + * @param FileRef|string $fileref_or_id + * @param string $user_id + * @return bool + */ + public function isFileWritable($fileref_or_id, $user_id) + { + return $this->isWritable($user_id); + } +} diff --git a/lib/models/Courseware/BlockTypes/TestBlock.php b/lib/models/Courseware/BlockTypes/TestBlock.php new file mode 100644 index 0000000..181fff6 --- /dev/null +++ b/lib/models/Courseware/BlockTypes/TestBlock.php @@ -0,0 +1,125 @@ + '']; + } + + /** + * Get the JSON schema for the payload of this block type. + */ + public static function getJsonSchema(): string + { + $schema = [ + 'type' => 'object', + 'properties' => [ + 'assignment' => [ + 'type' => 'string' + ] + ] + ]; + + return json_encode($schema); + } + + /** + * Get the list of categories for this block type. + */ + public static function getCategories(): array + { + return ['interaction']; + } + + /** + * Get the list of content types for this block type. + */ + public static function getContentTypes(): array + { + return ['rich']; + } + + /** + * Get the list of file types for this block type. + */ + public static function getFileTypes(): array + { + return []; + } + + /** + * Copy the payload of this block into the given range id. + */ + public function copyPayload(string $rangeId = ''): array + { + static $assignments = []; + + $context = $rangeId === $GLOBALS['user']->id ? 'user' : 'course'; + $payload = $this->getPayload(); + + if ($payload['assignment']) { + $assignment = VipsAssignment::find($payload['assignment']); + } + + if (!$assignment || !$assignment->checkEditPermission()) { + return $this->initialPayload(); + } + + if ($context === 'course' && !VipsModule::hasStatus('tutor', $rangeId)) { + return $this->initialPayload(); + } + + if ($assignment->range_id !== $rangeId) { + if (!isset($assignments[$assignment->id])) { + $copy = $assignment->copyIntoCourse($rangeId, $context); + $assignments[$assignment->id] = $copy->id; + } + + $payload['assignment'] = $assignments[$assignment->id]; + } + + return $payload; + } +} diff --git a/lib/models/FileRef.php b/lib/models/FileRef.php index 2a7f485..4196367 100644 --- a/lib/models/FileRef.php +++ b/lib/models/FileRef.php @@ -299,7 +299,6 @@ class FileRef extends SimpleORMap implements PrivacyObject, FeedbackRange return mb_strpos($this->mime_type, 'audio/') === 0; } - /** * Determines if the FileRef references a video file. * @@ -311,6 +310,22 @@ class FileRef extends SimpleORMap implements PrivacyObject, FeedbackRange } /** + * Get the preferred content disposition of this file. + */ + public function getContentDisposition(): string + { + if ($this->isImage() || $this->isAudio() || $this->isVideo()) { + return 'inline'; + } + + if (in_array($this->mime_type, ['application/pdf', 'text/plain'])) { + return 'inline'; + } + + return 'attachment'; + } + + /** * Export available data of a given user into a storage object * (an instance of the StoredUserData class) for that user. * diff --git a/lib/models/Folder.php b/lib/models/Folder.php index 1c7a13e..111decf 100644 --- a/lib/models/Folder.php +++ b/lib/models/Folder.php @@ -274,6 +274,17 @@ class Folder extends SimpleORMap implements FeedbackRange } /** + * Retrieves folders by range id and folder type. + * + * @param string $range_id range id of the folder + * @param string $folder_type folder type name + */ + public static function findByRangeIdAndFolderType(?string $range_id, string $folder_type) + { + return self::findBySQL('range_id = ? AND folder_type = ?', [$range_id, $folder_type]); + } + + /** * This callback is called before storing a Folder object. * In case the name field is changed this callback assures that the * name of the Folder object is unique inside the parent folder. @@ -381,11 +392,15 @@ class Folder extends SimpleORMap implements FeedbackRange * * @param string range_id The ID of the Stud.IP object whose top folder shall be found. * @param string folder_type The expected folder type related to the Stud.IP object (defaults to RootFolder, use MessageFolder for the top folder of a message) + * @param string range_type The expected range type of the Stud.IP object (defaults to auto detect) * * @returns Folder|null Folder object on success or null, if no folder can be created. **/ - public static function findTopFolder($range_id, $folder_type = 'RootFolder') - { + public static function findTopFolder( + string $range_id, + string $folder_type = 'RootFolder', + ?string $range_type = null + ) { $top_folder = self::findOneBySQL( "range_id = ? AND folder_type = ? AND parent_id=''", [$range_id, $folder_type] @@ -395,10 +410,12 @@ class Folder extends SimpleORMap implements FeedbackRange if (!$top_folder) { //top_folder doest not exist: create it //determine range type: - $range_type = self::findRangeTypeById($range_id); if (!$range_type) { - //no range type means we can't create a folder! - return null; + $range_type = self::findRangeTypeById($range_id); + if (!$range_type) { + //no range type means we can't create a folder! + return null; + } } $top_folder = self::createTopFolder($range_id, $range_type, $folder_type); diff --git a/lib/models/vips/ClozeTask.php b/lib/models/vips/ClozeTask.php new file mode 100644 index 0000000..b5c8069 --- /dev/null +++ b/lib/models/vips/ClozeTask.php @@ -0,0 +1,505 @@ +task['text'] = ''; + } + } + + /** + * Initialize this instance from the current request environment. + */ + public function initFromRequest($request): void + { + parent::initFromRequest($request); + + $this->parseClozeText(trim($request['cloze_text'])); + $this->task['compare'] = $request['compare']; + + if ($this->task['compare'] === 'numeric') { + $this->task['epsilon'] = (float) strtr($request['epsilon'], ',', '.') / 100; + } + + if (isset($request['input_width'])) { + $this->task['input_width'] = (int) $request['input_width']; + } + + if ($request['layout']) { + $this->task['layout'] = $request['layout']; + } + } + + /** + * Compute the default maximum points which can be reached in this + * exercise, dependent on the number of answers. + */ + public function itemCount(): int + { + return count($this->task['answers']); + } + + /** + * Return the list of keywords used for text export. The first keyword + * in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return ["L'text", 'Eingabehilfe', 'Abgleich']; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + parent::initText($exercise); + + $this->parseClozeText($this->description); + $this->description = ''; + + foreach ($exercise as $tag) { + if (key($tag) === 'Abgleich') { + if (current($tag) === 'Kleinbuchstaben') { + $this->task['compare'] = 'ignorecase'; + } + } + } + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + $this->task['text'] = ''; + $select = null; + + foreach ($exercise->items->item->description->children() as $name => $elem) { + if ($name == 'text') { + $this->task['text'] .= (string) $elem; + } else if ($name == 'answers') { + $answers = []; + + foreach ($elem->answer as $answer) { + $answers[] = [ + 'text' => (string) $answer, + 'score' => (string) $answer['score'] + ]; + } + + if ($elem['select'] == 'true') { + $select[] = $this->itemCount(); + } + + $this->task['answers'][] = $answers; + $this->task['text'] .= '[[]]'; + } + } + + $this->task['text'] = Studip\Markup::purifyHtml($this->task['text']); + + switch ($exercise->items->item['type']) { + case 'cloze-input': + $this->task['select'] = $select; + break; + case 'cloze-select': + $this->task['layout'] = 'select'; + break; + case 'cloze-drag': + $this->task['layout'] = 'drag'; + } + + if ($exercise->items->item->{'submission-hints'}) { + if ($exercise->items->item->{'submission-hints'}->input['width']) { + $this->task['input_width'] = (int) $exercise->items->item->{'submission-hints'}->input['width']; + } + } + + if ($exercise->items->item->{'evaluation-hints'}) { + switch ($exercise->items->item->{'evaluation-hints'}->similarity['type']) { + case 'ignorecase': + $this->task['compare'] = 'ignorecase'; + break; + case 'numeric': + $this->task['compare'] = 'numeric'; + $this->task['epsilon'] = (float) $exercise->items->item->{'evaluation-hints'}->{'input-data'}; + } + } + } + + /** + * Creates a template for editing a cloze exercise. NOTE: As a cloze + * exercise has no special fields (it consists only of the question), + * normally, an empty template will be returned. The only elements it can + * contain are message boxes alerting that for the same cloze an answer + * alternative has been set repeatedly. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + $duplicate_alternatives = $this->findDuplicateAlternatives(); + + foreach ($duplicate_alternatives as $alternative) { + $message = sprintf(_('Achtung: Sie haben bei der %d. Lücke die Antwort „%s“ mehrfach eingetragen.'), + $alternative['index'] + 1, htmlReady($alternative['text'])); + PageLayout::postWarning($message); + } + + return parent::getEditTemplate($assignment); + } + + /** + * Create a template for viewing an exercise. + */ + public function getViewTemplate($view, $solution, $assignment, $user_id): \Flexi\Template + { + $template = parent::getViewTemplate($view, $solution, $assignment, $user_id); + + if ($solution && $solution->id) { + $template->results = $this->evaluateItems($solution); + } + + return $template; + } + + /** + * Return the interaction type of this task (input, select or drag). + */ + public function interactionType(): string + { + return $this->task['layout'] ?? 'input'; + } + + /** + * Check if selection should be offered for the given item. + */ + public function isSelect(string $item, bool $use_default = true): bool + { + if ($use_default && $this->interactionType() === 'select') { + return true; + } + + if (isset($this->task['select'])) { + return in_array($item, $this->task['select']); + } + + return false; + } + + /** + * Returns all currently unassigned answers for the given solution. + */ + public function availableAnswers(?VipsSolution $solution): array + { + $answers = []; + $response = $solution->response ?? []; + + foreach ($this->task['answers'] as $answer) { + foreach ($answer as $option) { + $i = array_search($option['text'], $response); + + if ($i !== false) { + unset($response[$i]); + } else if ($option['text'] !== '') { + $answers[] = $option['text']; + } + } + } + + sort($answers, SORT_LOCALE_STRING); + return $answers; + } + + /** + * Returns all the correct answers for an item in an array. + */ + public function correctAnswers($item): array + { + $answers = []; + + foreach ($this->task['answers'][$item] as $answer) { + if ($answer['score'] == 1) { + $answers[] = $answer['text']; + } + } + + return $answers; + } + + /** + * Calculate the optimal input field size for text exercises. + * + * @param int $item item number + * @return int length of input field in characters + */ + public function getInputWidth($item): int + { + if (isset($this->task['input_width'])) { + return 5 << $this->task['input_width']; + } + + $max = 0; + + foreach ($this->task['answers'][$item] as $option) { + $length = mb_strlen($option['text']); + + if ($length > $max) { + $max = $length; + } + } + + $length = $max ? min(max($max, 6), 48) : 12; + + // possible sizes: 5, 10, 20, 40 + return 5 << ceil(log($length / 6) / log(2)); + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed $solution The solution XML string as returned by responseFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $response = $solution->response; + $ignorecase = isset($this->task['compare']) && $this->task['compare'] === 'ignorecase'; + $numeric = isset($this->task['compare']) && $this->task['compare'] === 'numeric'; + + foreach ($this->task['answers'] as $blank => $answer) { + $student_answer = $this->normalizeText($response[$blank] ?? '', $ignorecase); + $options = ['' => 0]; + $points = 0; + $safe = $this->interactionType() !== 'input'; + + foreach ($answer as $option) { // different answer options + if ($numeric && $student_answer !== '') { + $correct_unit = $student_unit = null; + $correct = $this->normalizeFloat($option['text'], $correct_unit); + $student = $this->normalizeFloat($response[$blank], $student_unit); + + if ($correct_unit === $student_unit) { + if (abs($correct - $student) <= abs($correct * $this->task['epsilon'])) { + $options[$student_answer] = max($option['score'], $options[$student_answer]); + } else { + $safe = true; + } + } + } else { + $content = $this->normalizeText($option['text'], $ignorecase); + $options[$content] = $option['score']; + } + } + + if (isset($options[$student_answer])) { + $points = $options[$student_answer]; + $safe = true; + } + + $result[] = ['points' => $points, 'safe' => $safe]; + } + + return $result; + } + + + + ####################################### + # # + # h e l p e r f u n c t i o n s # + # # + ####################################### + + + + /** + * Returns the exercise for the lecturer. Clozes are represented by square brackets. + */ + public function getClozeText(): string + { + $is_html = Studip\Markup::isHtml($this->task['text']); + $result = ''; + + foreach (explode('[[]]', $this->task['text']) as $blank => $text) { + $result .= $text; + + if (isset($this->task['answers'][$blank])) { // blank + $answers = []; + $select = $this->isSelect($blank, false) ? ':' : ''; + + foreach ($this->task['answers'][$blank] as $answer) { + $answer_text = $answer['text']; + + if (preg_match('/^$|^[":*~ ]|\||\]\]|[] ]$/', $answer_text)) { + $answer_text = '"' . $answer_text . '"'; + } + + if ($answer['score'] == 0) { + $answers[] = '*' . $answer_text; + } else if ($answer['score'] == 0.5) { + $answers[] = '~' . $answer_text; + } else { + $answers[] = $answer_text; + } + } + + $blank = '[[' . $select . implode('|', $answers) . ']]'; + + if ($is_html) { + $blank = htmlReady($blank); + } + + $result .= $blank; + } + } + + return $result; + } + + + + /** + * Converts plain text ("foo bar [blank] text...") to array. + */ + public function parseClozeText(string $question): void + { + $is_html = Studip\Markup::isHtml($question); + $question = Studip\Markup::purifyHtml($question); + $this->task['text'] = ''; + + // $question_array contains text elements and blanks (surrounded by [[ and ]]). + $parts = preg_split('/(\[\[(?:".*?"|.)*?\]\])/s', $question, -1, PREG_SPLIT_DELIM_CAPTURE); + $select = null; + + foreach ($parts as $part) { + if (preg_match('/^\[\[(.*)\]\]$/s', $part, $matches)) { + $part = preg_replace("/[\t\n\r\xA0]/", ' ', $matches[1]); + $answers = []; + + if ($is_html) { + $part = Studip\Markup::markAsHtml($part); + $part = Studip\Markup::removeHtml($part); + } + + if ($part[0] === ':') { + $select[] = $this->itemCount(); + $part = substr($part, 1); + } + + if ($part !== '') { + preg_match_all('/((?:".*?"|[^|])*)\|/', $part . '|', $matches); + + foreach ($matches[1] as $answer) { + $answer = trim($answer); + $points = 1; + + if ($answer !== '') { + if ($answer[0] === '*') { + $points = 0; + $answer = substr($answer, 1); + } else if ($answer[0] === '~') { + $points = 0.5; + $answer = substr($answer, 1); + } + } + + if (preg_match('/^"(.*)"$/', $answer, $matches)) { + $answer = $matches[1]; + } + + $answers[] = ['text' => $answer, 'score' => $points]; + } + } + + $this->task['answers'][] = $answers; + $this->task['text'] .= '[[]]'; + } else { + $this->task['text'] .= $part; + } + } + + $this->task['select'] = $select; + } + + /** + * Searches in each cloze if an answer alternative is given repatedly. + * + * @return array Either an empty array or an array of arrays, each containing the + * elements 'index' (index of the cloze where the duplicate + * entry was found) and 'text' (text of the duplicate entry). + */ + private function findDuplicateAlternatives(): array + { + $duplicate_alternatives = []; + + foreach ($this->task['answers'] as $index => $answers) { + $alternatives = []; + + foreach ($answers as $answer) { + if (in_array($answer['text'], $alternatives, true)) { + $duplicate_alternatives[] = [ + 'index' => $index, + 'text' => $answer['text'] + ]; + } + + $alternatives[] = $answer['text']; + } + } + + return $duplicate_alternatives; + } +} diff --git a/lib/models/vips/DummyExercise.php b/lib/models/vips/DummyExercise.php new file mode 100644 index 0000000..daa9dc5 --- /dev/null +++ b/lib/models/vips/DummyExercise.php @@ -0,0 +1,83 @@ + float, 'safe' => boolean). + */ + public function evaluateItems($solution): array + { + return []; + } + + /** + * Compute the default maximum points which can be reached in this + * exercise, dependent on the number of answers (defaults to 1). + */ + public function itemCount(): int + { + return 0; + } + + /** + * Create a template for editing an exercise. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + $template = $GLOBALS['template_factory']->open('shared/string'); + $template->content = ''; + + return $template; + } + + /** + * Create a template for viewing an exercise. + */ + public function getViewTemplate( + string $view, + ?VipsSolution $solution, + VipsAssignment $assignment, + ?string $user_id + ): Flexi\Template { + $template = $GLOBALS['template_factory']->open('shared/string'); + $template->content = ''; + + return $template; + } +} diff --git a/lib/models/vips/Exercise.php b/lib/models/vips/Exercise.php new file mode 100644 index 0000000..a4ef00a --- /dev/null +++ b/lib/models/vips/Exercise.php @@ -0,0 +1,855 @@ +, array> + */ + private static array $exercise_types = []; + + /** + * Configure the database mapping. + */ + protected static function configure($config = []) + { + $config['db_table'] = 'etask_tasks'; + + $config['serialized_fields']['options'] = JSONArrayObject::class; + + $config['has_and_belongs_to_many']['tests'] = [ + 'class_name' => VipsTest::class, + 'thru_table' => 'etask_test_tasks', + 'thru_key' => 'task_id', + 'thru_assoc_key' => 'test_id' + ]; + + $config['has_many']['exercise_refs'] = [ + 'class_name' => VipsExerciseRef::class, + 'assoc_foreign_key' => 'task_id' + ]; + $config['has_many']['solutions'] = [ + 'class_name' => VipsSolution::class, + 'assoc_foreign_key' => 'task_id', + 'on_delete' => 'delete' + ]; + + $config['has_one']['folder'] = [ + 'class_name' => Folder::class, + 'assoc_foreign_key' => 'range_id', + 'assoc_func' => 'findByRangeIdAndFolderType', + 'foreign_key' => fn($record) => [$record->id, 'ExerciseFolder'], + 'on_delete' => 'delete' + ]; + + $config['belongs_to']['user'] = [ + 'class_name' => User::class, + 'foreign_key' => 'user_id' + ]; + + parent::configure($config); + } + + /** + * Initialize a new instance of this class. + */ + public function __construct($id = null) + { + parent::__construct($id); + + if (!isset($id)) { + $this->type = get_class($this); + $this->task = ['answers' => []]; + } + + if (is_null($this->options)) { + $this->options = []; + } + } + + /** + * Initialize this instance from the current request environment. + */ + public function initFromRequest($request): void + { + $this->title = trim($request['exercise_name']); + $this->description = trim($request['exercise_question']); + $this->description = Studip\Markup::purifyHtml($this->description); + $exercise_hint = trim($request['exercise_hint']); + $exercise_hint = Studip\Markup::purifyHtml($exercise_hint); + $feedback = trim($request['feedback']); + $feedback = Studip\Markup::purifyHtml($feedback); + $this->task = ['answers' => []]; + $this->options = []; + + if ($this->title === '') { + $this->title = _('Aufgabe'); + } + + if ($exercise_hint !== '') { + $this->options['hint'] = $exercise_hint; + } + + if ($feedback !== '') { + $this->options['feedback'] = $feedback; + } + + if ($request['exercise_comment']) { + $this->options['comment'] = 1; + } + + if ($request['file_ids'] && !$request['files_visible']) { + $this->options['files_hidden'] = 1; + } + } + + /** + * Filter input from flexible input with HTMLPurifier (if required). + */ + public static function purifyFlexibleInput(string $html): string + { + if (Studip\Markup::isHtml($html)) { + $text = Studip\Markup::removeHtml($html); + + if (substr_count($html, '<') > 1 || kill_format($text) !== $text) { + $html = Studip\Markup::purifyHtml($html); + } else { + $html = $text; + } + } + + return $html; + } + + /** + * Load a specific exercise from the database. + */ + public static function find($id) + { + $db = DBManager::get(); + + $stmt = $db->prepare('SELECT * FROM etask_tasks WHERE id = ?'); + $stmt->execute([$id]); + $data = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($data) { + return self::buildExisting($data); + } + + return null; + } + + /** + * Load an array of exercises filtered by given sql from the database. + * + * @param string $sql clause to use on the right side of WHERE + * @param array $params for query + */ + public static function findBySQL($sql, $params = []) + { + $db = DBManager::get(); + + $has_join = stripos($sql, 'JOIN '); + if ($has_join === false || $has_join > 10) { + $sql = 'WHERE ' . $sql; + } + $stmt = $db->prepare('SELECT etask_tasks.* FROM etask_tasks ' . $sql); + $stmt->execute($params); + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $result = []; + + while ($data = $stmt->fetch()) { + $result[] = self::buildExisting($data); + } + + return $result; + } + + /** + * Find related records for an n:m relation (has_and_belongs_to_many) + * using a combination table holding the keys. + * + * @param string $foreign_key_value value of foreign key to find related records + * @param array $options relation options from other side of relation + */ + public static function findThru($foreign_key_value, $options) + { + $thru_table = $options['thru_table']; + $thru_key = $options['thru_key']; + $thru_assoc_key = $options['thru_assoc_key']; + + $sql = "JOIN `$thru_table` ON `$thru_table`.`$thru_assoc_key` = etask_tasks.id + WHERE `$thru_table`.`$thru_key` = ? " . $options['order_by']; + + return self::findBySQL($sql, [$foreign_key_value]); + } + + /** + * Create a new exercise object from a data array. + */ + public static function create($data) + { + $class = class_exists($data['type']) ? $data['type'] : DummyExercise::class; + + if (static::class === self::class) { + return $class::create($data); + } else { + return parent::create($data); + } + } + + /** + * Build an exercise object from a data array. + */ + public static function buildExisting($data) + { + $class = class_exists($data['type']) ? $data['type'] : DummyExercise::class; + + return $class::build($data, false); + } + + /** + * Initialize task structure from JSON string. + */ + public function setTask(mixed $value): void + { + if (is_string($value)) { + $this->content['task'] = $value; + $value = json_decode($value, true) ?: []; + } + + $this->task = $value; + } + + /** + * Restore this exercise from the database. + */ + public function restore() + { + $result = parent::restore(); + $this->setTask($this->task); + + return $result; + } + + /** + * Store this exercise into the database. + */ + public function store() + { + $this->content['task'] = json_encode($this->task); + + return parent::store(); + } + + /** + * Compute the default maximum points which can be reached in this + * exercise, dependent on the number of answers (defaults to 1). + */ + public function itemCount(): int + { + return 1; + } + + /** + * Overwrite this function for each exercise type where shuffling answer + * alternatives makes sense. + * + * @param string $user_id A value for initialising the randomizer. + */ + public function shuffleAnswers(string $user_id): void + { + } + + /** + * Returns true if this exercise type is considered as multiple choice. + * In this case, the evaluation mode set on the assignment is applied. + */ + public function isMultipleChoice(): bool + { + return false; + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param VipsSolution $solution The solution object returned by getSolutionFromRequest(). + */ + public abstract function evaluateItems(VipsSolution $solution): array; + + /** + * Evaluates a student's solution. + * + * @param VipsSolution $solution The solution object returned by getSolutionFromRequest(). + */ + public function evaluate(VipsSolution $solution): array + { + $results = $this->evaluateItems($solution); + $mc_mode = $solution->assignment->options['evaluation_mode']; + $malus = 0; + $points = 0; + $safe = true; + + foreach ($results as $item) { + if ($item['points'] === 0) { + ++$malus; + } else if ($item['points'] !== null) { + $points += $item['points']; + } + + if ($item['safe'] === null) { + $safe = null; + } else if ($safe !== null) { + // only true if all items are marked as 'safe' + $safe &= $item['safe']; + } + } + + if ($this->isMultipleChoice()) { + if ($mc_mode == 1) { + $points = max($points - $malus, 0); + } else if ($mc_mode == 2 && $malus > 0) { + $points = 0; + } + } + + $percent = $points / max(count($results), 1); + + return ['percent' => $percent, 'safe' => $safe]; + } + + /** + * Return the default response when there is no existing solution. + */ + public function defaultResponse(): array + { + return array_fill(0, $this->itemCount(), ''); + } + + /** + * Return the response of the student from the request POST data. + * + * @param array $request array containing the postdata for the solution. + */ + public function responseFromRequest(array|ArrayAccess $request): array + { + $result = []; + + for ($i = 0; $i < $this->itemCount(); ++$i) { + $result[] = trim($request['answer'][$i] ?? ''); + } + + return $result; + } + + /** + * Export a response for this exercise into an array of strings. + */ + public function exportResponse(array $response): array + { + return array_values($response); + } + + /** + * Export this exercise to Vips XML format. + */ + public function getXMLTemplate(VipsAssignment $assignment): Flexi\Template + { + return $this->getViewTemplate('xml', null, $assignment, null); + } + + /** + * Exercise handler to be called when a solution is corrected. + */ + public function correctSolutionAction(Trails\Controller$controller, VipsSolution $solution): void + { + } + + /** + * Return a URL to a specified route in this exercise class. + * $params can contain optional additional parameters. + */ + public function url_for($path, $params = []): string + { + $params['exercise_id'] = $this->id; + + return URLHelper::getURL('dispatch.php/vips/sheets/relay/' . $path, $params); + } + + /** + * Return an encoded URL to a specified route in this exercise class. + * $params can contain optional additional parameters. + */ + public function link_for($path, $params = []): string + { + return htmlReady($this->url_for($path, $params)); + } + + /** + * Create a template for editing an exercise. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + $template = VipsModule::$template_factory->open('exercises/' . $this->type . '/edit'); + $template->exercise = $this; + + return $template; + } + + /** + * Create a template for viewing an exercise. + */ + public function getViewTemplate( + string $view, + ?VipsSolution $solution, + VipsAssignment $assignment, + ?string $user_id + ): Flexi\Template { + if ($assignment->isShuffled() && $user_id) { + $this->shuffleAnswers($user_id); + } + + $template = VipsModule::$template_factory->open('exercises/' . $this->type . '/' . $view); + $template->exercise = $this; + $template->solution = $solution; + $template->response = $solution ? $solution->response : null; + $template->evaluation_mode = $assignment->options['evaluation_mode']; + + return $template; + } + + /** + * Return a template for solving an exercise. + */ + public function getSolveTemplate( + ?VipsSolution $solution, + VipsAssignment $assignment, + ?string $user_id + ): Flexi\Template { + return $this->getViewTemplate('solve', $solution, $assignment, $user_id); + } + + /** + * Return a template for correcting an exercise. + */ + public function getCorrectionTemplate(VipsSolution $solution): Flexi\Template + { + return $this->getViewTemplate('correct', $solution, $solution->assignment, $solution->user_id); + } + + /** + * Return a template for printing an exercise. + */ + public function getPrintTemplate(VipsSolution $solution, VipsAssignment $assignment, ?string $user_id) + { + return $this->getViewTemplate('print', $solution, $assignment, $user_id); + } + + /** + * Get the name of this exercise type. + */ + public function getTypeName(): string + { + return self::$exercise_types[$this->type]['name']; + } + + /** + * Get the icon of this exercise type. + */ + public static function getTypeIcon(string $role = Icon::DEFAULT_ROLE): Icon + { + return Icon::create('question-circle', $role); + } + + /** + * Get a description of this exercise type. + */ + public static function getTypeDescription(): string + { + return ''; + } + + /** + * Get the list of supported exercise types. + */ + public static function getExerciseTypes(): array + { + return self::$exercise_types; + } + + /** + * Register a new exercise type and class. + * + * @param class-string $class + */ + public static function addExerciseType(string $name, string $class, mixed $type = null): void + { + self::$exercise_types[$class] = compact('name', 'type'); + } + + /** + * Return the list of keywords used for legacy text export. The first + * keyword in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return []; + } + + /** + * Import a new exercise from text data array. + */ + public static function importText(string $segment): static + { + $all_keywords = ['Tipp']; + + $types = []; + foreach (self::$exercise_types as $key => $value) { + $keywords = $key::getTextKeywords(); + + if ($keywords) { + $all_keywords = array_merge($all_keywords, $keywords); + $types[$key] = array_shift($keywords); + } + } + + $type = ''; + $pattern = implode('|', array_unique($all_keywords)); + $parts = preg_split("/\n($pattern):/", $segment, -1, PREG_SPLIT_DELIM_CAPTURE); + $title = array_shift($parts); + + $exercise = [['Name' => trim($title)]]; + + if ($parts) { + $type = array_shift($parts); + $text = array_shift($parts); + $text = preg_replace('/\\\\' . $type . '$/', '', trim($text)); + + $exercise[] = ['Type' => trim($type)]; + $exercise[] = ['Text' => trim($text)]; + } + + while ($parts) { + $tag = array_shift($parts); + $val = array_shift($parts); + $val = preg_replace('/\\\\' . $tag . '$/', '', trim($val)); + + $exercise[] = [$tag => trim($val)]; + } + + foreach ($types as $key => $value) { + if (preg_match('/^' . $value . '$/', $type)) { + $exercise_type = $key; + } + } + + if (!isset($exercise_type)) { + throw new InvalidArgumentException(_('Unbekannter Aufgabentyp: ') . $type); + } + + /** @var class-string $exercise_type */ + $result = new $exercise_type(); + $result->initText($exercise); + return $result; + } + + /** + * Import a new exercise from Vips XML format. + */ + public static function importXML($exercise): static + { + $type = (string) $exercise->items->item[0]['type']; + + foreach (self::$exercise_types as $key => $value) { + if ($type === $value['type'] || is_array($value['type']) && in_array($type, $value['type'])) { + $exercise_type = $key; + } + } + + if (!isset($exercise_type)) { + throw new InvalidArgumentException(_('Unbekannter Aufgabentyp: ') . $type); + } + + if ( + $exercise_type === MultipleChoiceTask::class + && $exercise->items->item[0]->choices + ) { + $exercise_type = MatrixChoiceTask::class; + } + + /** @var class-string $exercise_type */ + $result = new $exercise_type(); + $result->initXML($exercise); + return $result; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + foreach ($exercise as $tag) { + if (key($tag) === 'Name') { + $this->title = current($tag) ?: _('Aufgabe'); + } + + if (key($tag) === 'Text') { + $this->description = Studip\Markup::purifyHtml(current($tag)); + } + + if (key($tag) === 'Tipp') { + $this->options['hint'] = Studip\Markup::purifyHtml(current($tag)); + } + } + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + $this->title = trim($exercise->title); + + if ($this->title === '') { + $this->title = _('Aufgabe'); + } + + if ($exercise->description) { + $this->description = Studip\Markup::purifyHtml(trim($exercise->description)); + } + + if ($exercise->hint) { + $this->options['hint'] = Studip\Markup::purifyHtml(trim($exercise->hint)); + } + + if ($exercise['feedback'] == 'true') { + $this->options['comment'] = 1; + } + + if ($exercise->{'file-refs'}['hidden'] == 'true') { + $this->options['files_hidden'] = 1; + } + + if ($exercise->items->item[0]->feedback) { + $this->options['feedback'] = Studip\Markup::purifyHtml(trim($exercise->items->item[0]->feedback)); + } + } + + /** + * Construct a new solution object from the request post data. + */ + public function getSolutionFromRequest($request, ?array $files = null): VipsSolution + { + $solution = new VipsSolution(); + $solution->exercise = $this; + $solution->user_id = $GLOBALS['user']->id; + $solution->response = $this->responseFromRequest($request); + $solution->student_comment = trim($request['student_comment']); + + return $solution; + } + + /** + * Include files referenced by URL into the exercise attachments and + * rewrite all corresponding URLs in the exercise text. + */ + public function includeFilesForExport(): void + { + if (!$this->folder || count($this->folder->file_refs) === 0) { + $this->options['files_hidden'] = 1; + } + + $this->description = $this->rewriteLinksForExport($this->description); + $this->options['hint'] = $this->rewriteLinksForExport($this->options['hint']); + $this->task = $this->rewriteLinksForExport($this->task); + } + + /** + * Return a normalized version of a string + * + * @param string $string string to be normalized + * @param boolean $lowercase make string lower case + * @return string The normalized string + */ + protected function normalizeText(string $string, bool $lowercase = true): string + { + // remove leading/trailing spaces + $string = trim($string); + + // compress white space + $string = preg_replace('/\s+/u', ' ', $string); + + // delete blanks before and after [](){}:;,.!?"=<>^*/+- + $string = preg_replace('/ *([][(){}:;,.!?"=<>^*\/+-]) */', '$1', $string); + + // convert to lower case if requested + return $lowercase ? mb_strtolower($string) : $string; + } + + /** + * Return a normalized version of a float (and optionally a unit) + * + * @param string $string string to be normalized + * @param string $unit will contain the unit text + * @return float The normalized value + */ + protected function normalizeFloat(string $string, string &$unit): float + { + static $si_scale = [ + 'T' => 12, + 'G' => 9, + 'M' => 6, + 'k' => 3, + 'h' => 2, + 'd' => -1, + 'c' => -2, + 'm' => -3, + 'µ' => -6, + 'μ' => -6, + 'n' => -9, + 'p' => -12 + ]; + + // normalize representation + $string = $this->normalizeText($string, false); + $string = str_replace('*10^', 'e', $string); + $string = preg_replace_callback('/(\d+)\/(\d+)/', function($m) { return $m[1] / $m[2]; }, $string); + $string = strtr($string, ',', '.'); + + // split into value and unit + preg_match('/^([-+0-9.e]*)(.*)/', $string, $matches); + $value = (float) $matches[1]; + $unit = trim($matches[2]); + + if ($unit) { + $prefix = mb_substr($unit, 0, 1); + $letter = mb_substr($unit, 1, 1); + + if (ctype_alpha($letter) && isset($si_scale[$prefix])) { + $value *= pow(10, $si_scale[$prefix]); + $unit = mb_substr($unit, 1); + } + } + + return $value; + } + + /** + * UTF-8 compatible version of standard PHP levenshtein function. + */ + protected function levenshtein(string $string1, string $string2): int + { + $mb_str1 = preg_split('//u', $string1, null, PREG_SPLIT_NO_EMPTY); + $mb_str2 = preg_split('//u', $string2, null, PREG_SPLIT_NO_EMPTY); + + $mb_len1 = count($mb_str1); + $mb_len2 = count($mb_str2); + + $dist = []; + for ($i = 0; $i <= $mb_len1; ++$i) { + $dist[$i][0] = $i; + } + for ($j = 0; $j <= $mb_len2; ++$j) { + $dist[0][$j] = $j; + } + + for ($i = 1; $i <= $mb_len1; $i++) { + for ($j = 1; $j <= $mb_len2; $j++) { + $dist[$i][$j] = min( + $dist[$i-1][$j] + 1, + $dist[$i][$j-1] + 1, + $dist[$i-1][$j-1] + ($mb_str1[$i-1] !== $mb_str2[$j-1] ? 1 : 0) + ); + } + } + + return $dist[$mb_len1][$mb_len2]; + } + + /** + * Scan the given string or array (recursively) for referenced file URLs + * and rewrite those links into URNs suitable for XML export. + */ + protected function rewriteLinksForExport(mixed $data): mixed + { + if (is_array($data)) { + foreach ($data as $key => $value) { + $data[$key] = $this->rewriteLinksForExport($value); + } + } else if (is_string($data) && Studip\Markup::isHtml($data)) { + $data = preg_replace_callback('/"\Khttps?:[^"]*/', function($match) { + $url = html_entity_decode($match[0]); + $url = preg_replace( + '%/download/(?:normal|force_download)/\d/(\w+)/.+%', + '/sendfile.php?file_id=$1', + $url + ); + [$url, $query] = explode('?', $url); + + if (is_internal_url($url) && basename($url) === 'sendfile.php') { + parse_str($query, $query_params); + $file_id = $query_params['file_id']; + $file_ref = FileRef::find($file_id); + + if ($file_ref && $this->folder->file_refs->find($file_id)) { + return 'urn:vips:file-ref:file-' . $file_ref->file_id; + } + + if ($file_ref) { + $folder = $file_ref->folder->getTypedFolder(); + + if ($folder->isFileDownloadable($file_ref, $GLOBALS['user']->id)) { + if (!$this->folder->file_refs->find($file_id)) { + $file = $file_ref->file; + // $this->files->append($file); + } + + return 'urn:vips:file-ref:file-' . $file_id->file_id; + } + } + } + + return $match[0]; + }, $data); + } + + return $data; + } + + /** + * Calculate the size parameter for a flexible input element. + * + * @param string $text contents of the input + */ + public function flexibleInputSize(?string $text): string + { + return str_contains($text, "\n") || Studip\Markup::isHtml($text) ? 'large' : 'small'; + } + + /** + * Calculate the optimal textarea height for text exercises. + * + * @param string $text contents of textarea + * @return int height of textarea in lines + */ + public function textareaSize(?string $text): int + { + return max(substr_count($text, "\n") + 3, 5); + } +} diff --git a/lib/models/vips/MatchingTask.php b/lib/models/vips/MatchingTask.php new file mode 100644 index 0000000..bb559e2 --- /dev/null +++ b/lib/models/vips/MatchingTask.php @@ -0,0 +1,341 @@ +task['groups'] = []; + } + } + + /** + * Initialize this instance from the current request environment. + */ + public function initFromRequest($request): void + { + parent::initFromRequest($request); + + $id = $request['id']; + $_id = $request['_id']; + + $this->task['groups'] = []; + $this->task['select'] = $request['multiple'] ? 'multiple' : 'single'; + + foreach ($request['default'] as $i => $group) { + $group = self::purifyFlexibleInput($group); + $answers = (array) $request['answer'][$i]; + + if (trim($group) != '') { + foreach ($answers as $j => $answer) { + $answer = self::purifyFlexibleInput($answer); + + if (trim($answer) != '') { + $this->task['answers'][] = [ + 'id' => (int) $id[$i][$j], + 'text' => trim($answer), + 'group' => count($this->task['groups']) + ]; + } + } + + $this->task['groups'][] = trim($group); + } + } + + // list of answers that must remain unassigned + foreach ($request['_answer'] as $i => $answer) { + $answer = self::purifyFlexibleInput($answer); + + if (trim($answer) != '') { + $this->task['answers'][] = [ + 'id' => (int) $_id[$i], + 'text' => trim($answer), + 'group' => -1 + ]; + } + } + + $this->createIds(); + } + + /** + * Genereate new IDs for all answers that do not yet have one. + */ + public function createIds(): void + { + $ids = [0 => true]; + + foreach ($this->task['answers'] as $i => &$answer) { + if (empty($answer['id'])) { + do { + $answer['id'] = rand(); + } while (isset($ids[$answer['id']])); + } + + $ids[$answer['id']] = true; + } + } + + /** + * Check if multiple assignment mode is enabled for this exercise. + */ + public function isMultiSelect(): bool + { + return isset($this->task['select']) && $this->task['select'] === 'multiple'; + } + + /** + * Compute the default maximum points which can be reached in this + * exercise, dependent on the number of answers. + */ + public function itemCount(): int + { + return count($this->task['answers']) - count($this->correctAnswers(-1)); + } + + /** + * Sort the list of answers by their ids. + */ + public function sortAnswersById(): void + { + usort( + $this->task['answers'], + fn($a, $b) => $a['id'] <=> $b['id'] + ); + } + + /** + * Returns all the correct answers for the given group. + */ + public function correctAnswers(string $group): array + { + $answers = []; + + foreach ($this->task['answers'] as $answer) { + if ($answer['group'] == $group) { + $answers[] = $answer['text']; + } + } + + return $answers; + } + + /** + * Check if this answer is a correct assignment to the given group. + */ + public function isCorrectAnswer(array $answer, string $group): bool + { + if ($answer['group'] == $group) { + return true; + } + + foreach ($this->task['answers'] as $_answer) { + if ($_answer['group'] == $group) { + if ($answer['text'] === $_answer['text']) { + return true; + } + } + } + + return false; + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed $solution The solution XML string as returned by responseFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $response = $solution->response; + $item_count = $this->itemCount(); + + foreach ($this->task['answers'] as $answer) { + $group = $response[$answer['id']] ?? -1; + + if ($group != -1) { + $points = $this->isCorrectAnswer($answer, $group) ? 1 : 0; + $result[] = ['points' => $points, 'safe' => true]; + } + } + + // assign no points for missing answers + while (count($result) < $item_count) { + $result[] = ['points' => 0, 'safe' => true]; + } + + return $result; + } + + /** + * Return the list of keywords used for text export. The first keyword + * in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return ['ZU-Frage', 'Vorgabe', 'Antwort', 'Distraktor']; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + parent::initText($exercise); + + foreach ($exercise as $tag) { + if (key($tag) === 'Vorgabe') { + $group = count($this->task['groups']); + $this->task['groups'][] = Studip\Markup::purifyHtml(current($tag)); + } + + if (key($tag) === 'Antwort' && isset($group)) { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(current($tag)), + 'group' => $group + ]; + unset($group); + } + + if (key($tag) === 'Distraktor') { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(current($tag)), + 'group' => -1 + ]; + } + } + + $this->createIds(); + } + + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + + $this->task['select'] = $exercise->items->item['type'] == 'matching-multiple' ? 'multiple' : 'single'; + + foreach ($exercise->items->item->choices->choice as $choice) { + $this->task['groups'][] = Studip\Markup::purifyHtml(trim($choice)); + } + + foreach ($exercise->items->item->answers->answer as $answer) { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(trim($answer)), + 'group' => (int) $answer['correct'] + ]; + } + + $this->createIds(); + } + + + + /** + * Creates a template for editing a MatchingTask. + */ + public function getEditTemplate(?VipsAssignment $assignment): \Flexi\Template + { + if (!$this->task['answers']) { + foreach (range(0, 4) as $i) { + $this->task['answers'][] = ['id' => '', 'text' => '', 'group' => count($this->task['groups'])]; + $this->task['groups'][] = ''; + } + } + + return parent::getEditTemplate($assignment); + } + + /** + * Return the solution of the student from the request POST data. + * + * @param array $request array containing the postdata for the solution. + * @return array containing the solutions of the student. + */ + public function responseFromRequest(array|ArrayAccess $request): array + { + $result = []; + + foreach ($this->task['answers'] as $answer) { + // get the group the user has added this answer to + $result[$answer['id']] = (int) $request['answer'][$answer['id']]; + } + + return $result; + } + + /** + * Export a response for this exercise into an array of strings. + */ + public function exportResponse(array $response): array + { + $result = []; + + foreach ($this->task['answers'] as $answer) { + if ($answer['group'] != -1) { + if (isset($response[$answer['id']]) && $response[$answer['id']] != -1) { + $result[] = $this->task['groups'][$response[$answer['id']]]; + } else { + $result[] = ''; + } + } + } + + return $result; + } +} diff --git a/lib/models/vips/MatrixChoiceTask.php b/lib/models/vips/MatrixChoiceTask.php new file mode 100644 index 0000000..1ba2d90 --- /dev/null +++ b/lib/models/vips/MatrixChoiceTask.php @@ -0,0 +1,268 @@ +task['choices'] = []; + } + } + + /** + * Initialize this instance from the current request environment. + */ + public function initFromRequest($request): void + { + parent::initFromRequest($request); + + $this->task['choices'] = []; + $choice_index = []; + + foreach ($request['choice'] as $i => $choice) { + if (trim($choice) != '') { + $this->task['choices'][] = trim($choice); + $choice_index[$i] = count($choice_index); + } + } + + foreach ($request['answer'] as $i => $answer) { + $answer = self::purifyFlexibleInput($answer); + + if (trim($answer) != '') { + $this->task['answers'][] = [ + 'text' => trim($answer), + 'choice' => $choice_index[$request['correct'][$i]] + ]; + } + } + + if ($request['optional']) { + $this->options['optional'] = 1; + } + } + + /** + * Compute the default maximum points which can be reached in this + * exercise, dependent on the number of answers. + */ + public function itemCount(): int + { + return count($this->task['answers']); + } + + /** + * Shuffle the answer alternatives. + * + * @param $user_id string used for initialising the randomizer. + */ + public function shuffleAnswers(string $user_id): void + { + srand(crc32($this->id . ':' . $user_id)); + + $random_order = range(0, $this->itemCount() - 1); + shuffle($random_order); + + $answer_temp = []; + foreach ($random_order as $index) { + $answer_temp[$index] = $this->task['answers'][$index]; + } + $this->task['answers'] = $answer_temp; + + srand(); + } + + /** + * Returns true if this exercise type is considered as multiple choice. + * In this case, the evaluation mode set on the assignment is applied. + */ + public function isMultipleChoice(): bool + { + return true; + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed solution The solution XML string as returned by responseFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $response = $solution->response; + + foreach ($this->task['answers'] as $i => $answer) { + if (!isset($response[$i]) || $response[$i] === '' || $response[$i] == -1) { + $points = null; + } else { + $points = $response[$i] == $answer['choice'] ? 1 : 0; + } + + $result[] = ['points' => $points, 'safe' => true]; + } + + return $result; + } + + /** + * Return the list of keywords used for text export. The first keyword + * in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return ['MCO-Frage', 'Auswahl', '[+~]?Antwort']; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + parent::initText($exercise); + + foreach ($exercise as $tag) { + if (key($tag) === '+Antwort') { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(current($tag)), + 'choice' => 0 + ]; + } else if (key($tag) === 'Antwort') { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(current($tag)), + 'choice' => 1 + ]; + } + } + + foreach ($exercise as $tag) { + if (key($tag) === 'Auswahl') { + [$label_yes, $label_no] = explode('/', current($tag)); + $this->task['choices'] = [trim($label_yes), trim($label_no)]; + } + } + + $this->options['optional'] = 1; + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + + foreach ($exercise->items->item->answers->answer as $answer) { + if (isset($answer['correct'])) { + $choice = (int) $answer['correct']; + } else { + $choice = (int) $answer['score'] ? 0 : 1; + } + + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(trim($answer)), + 'choice' => $choice + ]; + } + + foreach ($exercise->items->item->choices->choice as $choice) { + if ($choice['type'] == 'none') { + $this->options['optional'] = 1; + } else { + $this->task['choices'][] = trim($choice); + } + } + } + + /** + * Creates a template for editing an exercise. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + if (!$this->task['choices']) { + $this->task['choices'] = [_('Ja'), _('Nein')]; + } + + if (!$this->task['answers']) { + $this->task['answers'] = array_fill(0, 5, ['text' => '', 'choice' => 0]); + } + + return parent::getEditTemplate($assignment); + } + + /** + * Create a template for viewing an exercise. + */ + public function getViewTemplate($view, $solution, $assignment, $user_id): Flexi\Template + { + $template = parent::getViewTemplate($view, $solution, $assignment, $user_id); + + if (isset($this->options['optional']) && $this->options['optional']) { + $template->optional_choice = [-1 => _('keine Antwort')]; + } else { + $template->optional_choice = []; + } + + return $template; + } + + /** + * Export a response for this exercise into an array of strings. + */ + public function exportResponse(array $response): array + { + return array_map( + fn($a) => $a == -1 ? '' : $a, + $response + ); + } +} diff --git a/lib/models/vips/MultipleChoiceTask.php b/lib/models/vips/MultipleChoiceTask.php new file mode 100644 index 0000000..68470ef --- /dev/null +++ b/lib/models/vips/MultipleChoiceTask.php @@ -0,0 +1,196 @@ + $answer) { + $answer = self::purifyFlexibleInput($answer); + + if (trim($answer) != '') { + $this->task['answers'][] = [ + 'text' => trim($answer), + 'score' => (int) $request['correct'][$i] + ]; + } + } + } + + /** + * Compute the default maximum points which can be reached in this + * exercise, dependent on the number of answers. + */ + public function itemCount(): int + { + return count($this->task['answers']); + } + + /** + * Return the default response when there is no existing solution. + */ + public function defaultResponse(): array + { + return []; + } + + /** + * Shuffle the answer alternatives. + * + * @param $user_id string used for initialising the randomizer. + */ + public function shuffleAnswers(string $user_id): void + { + srand(crc32($this->id . ':' . $user_id)); + + $random_order = range(0, $this->itemCount() - 1); + shuffle($random_order); + + $answer_temp = []; + foreach ($random_order as $index) { + $answer_temp[$index] = $this->task['answers'][$index]; + } + $this->task['answers'] = $answer_temp; + + srand(); + } + + /** + * Returns true if this exercise type is considered as multiple choice. + * In this case, the evaluation mode set on the assignment is applied. + */ + public function isMultipleChoice(): bool + { + return true; + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed $solution The solution XML string as returned by responseFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $response = $solution->response; + + foreach ($this->task['answers'] as $i => $answer) { + if (!isset($response[$i])) { + $points = null; + } else { + $points = (int) $response[$i] == $answer['score'] ? 1 : 0; + } + + $result[] = ['points' => $points, 'safe' => true]; + } + + return $result; + } + + /** + * Return the list of keywords used for text export. The first keyword + * in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return ['MC-Frage', '[+~]?Antwort']; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + parent::initText($exercise); + + foreach ($exercise as $tag) { + if (key($tag) === '+Antwort') { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(current($tag)), + 'score' => 1 + ]; + } else if (key($tag) === 'Antwort') { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(current($tag)), + 'score' => 0 + ]; + } + } + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + + foreach ($exercise->items->item->answers->answer as $answer) { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(trim($answer)), + 'score' => (int) $answer['score'] + ]; + } + } + + /** + * Creates a template for editing a MultipleChoiceTask. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + if (!$this->task['answers']) { + $this->task['answers'] = array_fill(0, 5, ['text' => '', 'score' => 0]); + } + + return parent::getEditTemplate($assignment); + } +} diff --git a/lib/models/vips/SequenceTask.php b/lib/models/vips/SequenceTask.php new file mode 100644 index 0000000..696fe6a --- /dev/null +++ b/lib/models/vips/SequenceTask.php @@ -0,0 +1,255 @@ + $answer) { + $answer = self::purifyFlexibleInput($answer); + + if (trim($answer) != '') { + $this->task['answers'][] = [ + 'id' => (int) $request['id'][$i], + 'text' => trim($answer) + ]; + } + } + + $this->task['compare'] = $request['compare']; + + $this->createIds(); + } + + /** + * Genereate new IDs for all answers that do not yet have one. + */ + public function createIds(): void + { + $ids = [0 => true]; + + foreach ($this->task['answers'] as $i => &$answer) { + if (empty($answer['id'])) { + do { + $answer['id'] = rand(); + } while (isset($ids[$answer['id']])); + } + + $ids[$answer['id']] = true; + } + } + + /** + * Compute the default maximum points which can be reached in this + * exercise, dependent on the number of answers. + */ + public function itemCount(): int + { + if ($this->task['compare'] === 'sequence') { + return max(count($this->task['answers']) - 1, 0); + } + + return count($this->task['answers']); + } + + /** + * Return the list of answers as ordered by the student (if applicable). + */ + public function orderedAnswers($response) + { + $answers = $this->task['answers']; + $pos = isset($response) ? array_flip($response) : []; + + usort($answers, function($a, $b) use ($pos) { + if (isset($pos[$a['id']]) && isset($pos[$b['id']])) { + return $pos[$a['id']] <=> $pos[$b['id']]; + } else if (isset($pos[$a['id']])) { + return -1; + } else if (isset($pos[$b['id']])) { + return 1; + } else { + return $a['id'] <=> $b['id']; + } + }); + + return $answers; + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed $solution The solution object returned by getSolutionFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $response = $solution->response; + $item_count = $this->itemCount(); + $answers = $this->task['answers']; + $pos = array_flip($response); + + for ($i = 0; $i < $item_count; ++$i) { + if ($this->task['compare'] === 'sequence') { + if ($pos[$answers[$i]['id']] + 1 == $pos[$answers[$i + 1]['id']]) { + $points = 1; + } else { + $points = 0; + } + } else { + if ($pos[$answers[$i]['id']] == $i) { + $points = 1; + } else { + $points = 0; + } + } + + if (!$this->task['compare'] && count($result)) { + $result[0]['points'] &= $points; + } else { + $result[] = ['points' => $points, 'safe' => true]; + } + } + + return $result; + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + + foreach ($exercise->items->item->answers->answer as $answer) { + $this->task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(trim($answer)) + ]; + } + + if ($exercise->items->item->{'evaluation-hints'}) { + switch ($exercise->items->item->{'evaluation-hints'}->similarity['type']) { + case 'position': + case 'sequence': + $this->task['compare'] = (string) $exercise->items->item->{'evaluation-hints'}->similarity['type']; + } + } + + $this->createIds(); + } + + + + /** + * Creates a template for editing a SequenceTask. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + if (!$this->task['answers']) { + $this->task['answers'] = array_fill(0, 5, ['id' => '', 'text' => '']); + } + + return parent::getEditTemplate($assignment); + } + + /** + * Create a template for viewing an exercise. + */ + public function getViewTemplate( + string $view, + ?VipsSolution $solution, + VipsAssignment $assignment, + ?string $user_id + ): Flexi\Template { + $template = parent::getViewTemplate($view, $solution, $assignment, $user_id); + + if ($solution && $solution->id) { + $template->results = $this->evaluateItems($solution); + } + + return $template; + } + + /** + * Return the solution of the student from the request POST data. + * + * @param array $request array containing the postdata for the solution. + * @return array containing the solutions of the student. + */ + public function responseFromRequest(array|ArrayAccess $request): array + { + $result = []; + + foreach ($request['answer'] as $id) { + $result[] = (int) $id; + } + + return $result; + } + + /** + * Export a response for this exercise into an array of strings. + */ + public function exportResponse(array $response): array + { + $result = []; + + foreach ($response as $id) { + foreach ($this->task['answers'] as $answer) { + if ($answer['id'] === $id) { + $result[] = $answer['text']; + } + } + } + + return $result; + } +} diff --git a/lib/models/vips/SingleChoiceTask.php b/lib/models/vips/SingleChoiceTask.php new file mode 100644 index 0000000..4029a65 --- /dev/null +++ b/lib/models/vips/SingleChoiceTask.php @@ -0,0 +1,279 @@ +task = []; + } + } + + /** + * Initialize this instance from the current request environment. + */ + public function initFromRequest($request): void + { + parent::initFromRequest($request); + + $this->task = []; + + foreach ($request['answer'] as $group => $answergroup) { + $task = []; + $description = trim($request['description'][$group]); + $description = Studip\Markup::purifyHtml($description); + + if ($this->task && $description != '') { + $task['description'] = $description; + } + + foreach ($answergroup as $i => $answer) { + $answer = self::purifyFlexibleInput($answer); + + if (trim($answer) != '') { + $task['answers'][] = [ + 'text' => trim($answer), + 'score' => $request['correct'][$group] == $i ? 1 : 0 + ]; + } + } + + if ($task) { + $this->task[] = $task; + } + } + + if ($request['optional']) { + $this->options['optional'] = 1; + } + } + + /** + * Computes the default maximum points which can be reached in this + * exercise, dependent on the number of groups. + * + * @return int maximum points + */ + public function itemCount(): int + { + return count($this->task); + } + + /** + * Shuffle the answer alternatives. + * + * @param $user_id string used for initialising the randomizer. + */ + public function shuffleAnswers(string $user_id): void + { + srand(crc32($this->id . ':' . $user_id)); + + for ($block = 0; $block < count($this->task); $block++) { + $random_order = range(0, count($this->task[$block]['answers']) - 1); + shuffle($random_order); + + $answer_temp = []; + foreach ($random_order as $index) { + $answer_temp[$index] = $this->task[$block]['answers'][$index]; + } + $this->task[$block]['answers'] = $answer_temp; + } + + srand(); + } + + /** + * Returns true if this exercise type is considered as multiple choice. + * In this case, the evaluation mode set on the assignment is applied. + */ + public function isMultipleChoice(): bool + { + return true; + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed $solution The solution XML string as returned by responseFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $response = $solution->response; + + foreach ($this->task as $i => $task) { + if (!isset($response[$i]) || $response[$i] === '' || $response[$i] == -1) { + $points = null; + } else { + $points = $task['answers'][$response[$i]]['score']; + } + + $result[] = ['points' => $points, 'safe' => true]; + } + + return $result; + } + + /** + * Return the list of keywords used for text export. The first keyword + * in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return ['SCO?-Frage|JN-Frage', '[+~]?Antwort']; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + parent::initText($exercise); + + $block = 0; + + foreach ($exercise as $tag) { + if (key($tag) === 'Type' && current($tag) === 'SCO-Frage') { + $this->options['optional'] = 1; + } + + if (key($tag) === '+Antwort' || key($tag) === 'Antwort') { + if (preg_match('/\n--$/', current($tag))) { + $text = trim(substr(current($tag), 0, -3)); + $incr = 1; + } else { + $text = current($tag); + $incr = 0; + } + + $score = key($tag) === '+Antwort' ? 1 : 0; + + $this->task[$block]['answers'][] = [ + 'text' => Studip\Markup::purifyHtml($text), + 'score' => $score + ]; + + $block += $incr; + } + } + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + + foreach ($exercise->items->item as $item) { + $task = []; + + if ($item->description) { + $task['description'] = Studip\Markup::purifyHtml(trim($item->description->text)); + } + + foreach ($item->answers->answer as $answer) { + if ($answer['default'] == 'true') { + $this->options['optional'] = 1; + } else { + $task['answers'][] = [ + 'text' => Studip\Markup::purifyHtml(trim($answer)), + 'score' => (int) $answer['score'] + ]; + } + } + + $this->task[] = $task; + } + } + + /** + * Creates a template for editing a SingleChoiceTask. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + if (!$this->task) { + $this->task[0]['answers'] = array_fill(0, 5, ['text' => '', 'score' => 0]); + } + + return parent::getEditTemplate($assignment); + } + + /** + * Create a template for viewing an exercise. + */ + public function getViewTemplate( + string $view, + ?VipsSolution $solution, + VipsAssignment $assignment, + ?string $user_id + ): Flexi\Template { + $template = parent::getViewTemplate($view, $solution, $assignment, $user_id); + + if (isset($this->options['optional']) && $this->options['optional']) { + $template->optional_answer = [-1 => ['text' => _('keine Antwort'), 'score' => 0]]; + } else { + $template->optional_answer = []; + } + + return $template; + } + + /** + * Export a response for this exercise into an array of strings. + */ + public function exportResponse(array $response): array + { + return array_map(function($a) { return $a == -1 ? '' : $a; }, $response); + } +} diff --git a/lib/models/vips/TextLineTask.php b/lib/models/vips/TextLineTask.php new file mode 100644 index 0000000..4a2e7d2 --- /dev/null +++ b/lib/models/vips/TextLineTask.php @@ -0,0 +1,271 @@ + $answer) { + if (trim($answer) != '') { + $this->task['answers'][] = [ + 'text' => trim($answer), + 'score' => (float) $request['correct'][$i] + ]; + } + } + + $this->task['compare'] = $request['compare']; + + if ($this->task['compare'] === 'numeric') { + $this->task['epsilon'] = (float) strtr($request['epsilon'], ',', '.') / 100; + } + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed $solution The solution XML string as returned by responseFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $response = $solution->response; + $studentSolution = $response[0]; + + $similarity = 0; + $safe = false; + $studentSolution = $this->normalizeText($studentSolution, true); + + if ($studentSolution === '') { + $result[] = ['points' => 0, 'safe' => true]; + return $result; + } + + foreach ($this->task['answers'] as $answer) { + $musterLoesung = $this->normalizeText($answer['text'], true); + $similarity_temp = 0; + + if ($musterLoesung === $studentSolution) { + $similarity_temp = 1; + } else if ($this->task['compare'] === 'levenshtein') { // Levenshtein-Distanz + $string1 = mb_substr($studentSolution, 0, 255); + $string2 = mb_substr($musterLoesung, 0, 255); + $divisor = max(mb_strlen($string1), mb_strlen($string2)); + + $levenshtein = $this->levenshtein($string1, $string2) / $divisor; + $similarity_temp = 1 - $levenshtein; + } else if ($this->task['compare'] === 'soundex') { // Soundex-Aussprache + $levenshtein = levenshtein(soundex($musterLoesung), soundex($studentSolution)); + + if ($levenshtein == 0) { + $similarity_temp = 0.8; + } else if ($levenshtein == 1) { + $similarity_temp = 0.6; + } else if ($levenshtein == 2) { + $similarity_temp = 0.4; + } else if ($levenshtein == 3) { + $similarity_temp = 0.2; + } else {// $levenshtein == 4 + $similarity_temp = 0; + } + } else if ($this->task['compare'] === 'numeric') { + $correct = $this->normalizeFloat($answer['text'], $correct_unit); + $student = $this->normalizeFloat($response[0], $student_unit); + + if ($correct_unit === $student_unit) { + if (abs($correct - $student) <= abs($correct * $this->task['epsilon'])) { + $similarity_temp = 1; + } else { + $safe = true; + } + } + } + + if ($answer['score'] == 1) { // correct + if ($similarity_temp > $similarity) { + $similarity = $similarity_temp; + $safe = $similarity_temp == 1; + } + } else if ($answer['score'] == 0.5) { // half correct + if ($similarity_temp > $similarity) { + $similarity = $similarity_temp * 0.5; + $safe = $similarity_temp == 1; + } + } else if ($similarity_temp == 1) { // false + $similarity = 0; + $safe = true; + break; + } + } + + $result[] = ['points' => $similarity, 'safe' => $safe]; + + return $result; + } + + /** + * Return the list of keywords used for text export. The first keyword + * in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return ['Frage', 'Eingabehilfe', 'Abgleich', '[+~]?Antwort']; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + parent::initText($exercise); + + foreach ($exercise as $tag) { + if (key($tag) === 'Abgleich') { + if (current($tag) === 'Levenshtein') { + $this->task['compare'] = 'levenshtein'; + } else if (current($tag) === 'Soundex') { + $this->task['compare'] = 'soundex'; + } + } + + if (key($tag) === '+Antwort') { + $this->task['answers'][] = [ + 'text' => current($tag), + 'score' => 1 + ]; + } else if (key($tag) === '~Antwort') { + $this->task['answers'][] = [ + 'text' => current($tag), + 'score' => 0.5 + ]; + } else if (key($tag) === 'Antwort') { + $this->task['answers'][] = [ + 'text' => current($tag), + 'score' => 0 + ]; + } + } + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + + foreach ($exercise->items->item->answers->answer as $answer) { + $this->task['answers'][] = [ + 'text' => trim($answer), + 'score' => (float) $answer['score'] + ]; + } + + if ($exercise->items->item->{'evaluation-hints'}) { + switch ($exercise->items->item->{'evaluation-hints'}->similarity['type']) { + case 'levenshtein': + case 'soundex': + $this->task['compare'] = (string) $exercise->items->item->{'evaluation-hints'}->similarity['type']; + break; + case 'numeric': + $this->task['compare'] = 'numeric'; + $this->task['epsilon'] = (float) $exercise->items->item->{'evaluation-hints'}->{'input-data'}; + } + } + } + + /** + * Creates a template for editing a TextLineTask. + */ + public function getEditTemplate(?VipsAssignment $assignment): Flexi\Template + { + if (!$this->task['answers']) { + $this->task['answers'] = array_fill(0, 5, ['text' => '', 'score' => 0]); + } + + return parent::getEditTemplate($assignment); + } + + /** + * Create a template for viewing an exercise. + */ + public function getViewTemplate( + string $view, + ?VipsSolution $solution, + VipsAssignment $assignment, + ?string $user_id + ): Flexi\Template { + $template = parent::getViewTemplate($view, $solution, $assignment, $user_id); + + if ($solution && $solution->id) { + $template->results = $this->evaluateItems($solution); + } + + return $template; + } + + /** + * Returns all the correct answers in an array. + */ + public function correctAnswers(): array + { + $answers = []; + + foreach ($this->task['answers'] as $answer) { + if ($answer['score'] == 1) { + $answers[] = $answer['text']; + } + } + + return $answers; + } +} diff --git a/lib/models/vips/TextTask.php b/lib/models/vips/TextTask.php new file mode 100644 index 0000000..5684195 --- /dev/null +++ b/lib/models/vips/TextTask.php @@ -0,0 +1,279 @@ +task['answers'][0] = [ + 'text' => Studip\Markup::purifyHtml(trim($request['answer_0'])), + 'score' => 1 + ]; + + $this->task['template'] = trim($request['answer_default']); + $this->task['compare'] = $request['compare']; + + if ($request['layout']) { + $this->task['layout'] = $request['layout']; + } + + if ($request['layout'] === 'markup') { + $this->task['template'] = Studip\Markup::purifyHtml($this->task['template']); + } + + if ($request['file_upload'] || $request['layout'] === 'none') { + $this->options['file_upload'] = 1; + } + } + + /** + * Exercise handler to be called when a solution is corrected. + */ + public function correctSolutionAction(Trails\Controller $controller, VipsSolution $solution): void + { + $commented_solution = Request::get('commented_solution'); + + if (isset($commented_solution)) { + $solution->commented_solution = Studip\Markup::purifyHtml(trim($commented_solution)); + } else { + $solution->commented_solution = null; + } + + if (Request::submitted('delete_commented_solution')) { + $solution->commented_solution = null; + $solution->store(); + + PageLayout::postSuccess(_('Die kommentierte Lösung wurde gelöscht.')); + } + } + + /** + * Return the layout of this task (text, markup, code or none). + */ + public function getLayout(): string + { + return $this->task['layout'] ?? 'text'; + } + + /** + * Evaluates a student's solution for the individual items in this + * exercise. Returns an array of ('points' => float, 'safe' => boolean). + * + * @param mixed $solution The solution XML string as returned by responseFromRequest(). + */ + public function evaluateItems($solution): array + { + $result = []; + + $answerDefault = Studip\Markup::removeHtml($this->task['template']); + $musterLoesung = Studip\Markup::removeHtml($this->task['answers'][0]['text']); + $studentSolution = Studip\Markup::removeHtml($solution->response[0]); + + $answerDefault = $this->normalizeText($answerDefault, true); + $studentSolution = $this->normalizeText($studentSolution, true); + $musterLoesung = $this->normalizeText($musterLoesung, true); + + if ($studentSolution == '' || $studentSolution == $answerDefault) { + $has_files = $solution->folder && count($solution->folder->file_refs); + $result[] = ['points' => 0, 'safe' => !$has_files ? true : null]; + } else if ($musterLoesung == $studentSolution) { + $result[] = ['points' => 1, 'safe' => true]; + } else if ($this->task['compare'] === 'levenshtein') { + $string1 = mb_substr($studentSolution, 0, 500); + $string2 = mb_substr($musterLoesung, 0, 500); + $string3 = mb_substr($answerDefault, 0, 500); + $divisor = $this->levenshtein($string3, $string2) ?: 1; + + $levenshtein = $this->levenshtein($string1, $string2) / $divisor; + $similarity = max(1 - $levenshtein, 0); + $result[] = ['points' => $similarity, 'safe' => false]; + } else { + $result[] = ['points' => 0, 'safe' => null]; + } + + return $result; + } + + /** + * Return the default response when there is no existing solution. + */ + public function defaultResponse(): array + { + return [$this->task['template']]; + } + + /** + * Return the solution of the student from the request POST data. + * + * @param array $request array containing the postdata for the solution. + * @return array containing the solutions of the student. + */ + public function responseFromRequest(array|ArrayAccess $request): array + { + $result = parent::responseFromRequest($request); + + if ($this->getLayout() === 'markup') { + $result = array_map('Studip\Markup::purifyHtml', $result); + } + + return $result; + } + + /** + * Construct a new solution object from the request post data. + */ + public function getSolutionFromRequest($request, ?array $files = null): VipsSolution + { + $solution = parent::getSolutionFromRequest($request, $files); + $upload = $files['upload'] ?: ['name' => []]; + $solution_files = []; + + if ($this->options['file_upload']) { + if ($files['upload']) { + $solution->options['upload'] = $files['upload']; + } + + $solution->store(); + $folder = Folder::findTopFolder($solution->id, 'ResponseFolder', 'response'); + + if (is_array($request['file_ids'])) { + foreach ($request['file_ids'] as $file_id) { + $file_ref = FileRef::find($file_id); + FileManager::copyFile($file_ref->getFileType(), $folder->getTypedFolder(), User::findCurrent()); + } + } + + FileManager::handleFileUpload($upload, $folder->getTypedFolder()); + } + + return $solution; + } + + /** + * Return the list of keywords used for text export. The first keyword + * in the list must be the keyword for the exercise type. + */ + public static function getTextKeywords(): array + { + return ['Offene Frage', 'Eingabehilfe', 'Abgleich', 'Vorgabe', 'Antwort']; + } + + /** + * Initialize this instance from the given text data array. + */ + public function initText(array $exercise): void + { + parent::initText($exercise); + + foreach ($exercise as $tag) { + if (key($tag) === 'Abgleich') { + if (current($tag) === 'Levenshtein') { + $this->task['compare'] = 'levenshtein'; + } + } + + if (key($tag) === 'Vorgabe') { + $this->task['template'] = Studip\Markup::purifyHtml(current($tag)); + } + + if (key($tag) === 'Antwort') { + $this->task['answers'][0] = [ + 'text' => Studip\Markup::purifyHtml(current($tag)), + 'score' => 1 + ]; + } + } + } + + /** + * Initialize this instance from the given SimpleXMLElement object. + */ + public function initXML($exercise): void + { + parent::initXML($exercise); + + foreach ($exercise->items->item->answers->answer as $answer) { + if ($answer['score'] == '1') { + $this->task['answers'][0] = [ + 'text' => Studip\Markup::purifyHtml(trim($answer)), + 'score' => 1 + ]; + } else if ($answer['default'] == 'true') { + $this->task['template'] = Studip\Markup::purifyHtml(trim($answer)); + } + } + + if ($exercise->items->item->{'evaluation-hints'}) { + switch ($exercise->items->item->{'evaluation-hints'}->similarity['type']) { + case 'levenshtein': + $this->task['compare'] = 'levenshtein'; + } + } + + if ($exercise->items->item->{'submission-hints'}->input) { + switch ($exercise->items->item->{'submission-hints'}->input['type']) { + case 'markup': + $this->task['layout'] = 'markup'; + break; + case 'code': + $this->task['layout'] = 'code'; + break; + case 'none': + $this->task['layout'] = 'none'; + } + } + + if ($exercise->items->item->{'submission-hints'}->attachments) { + if ($exercise->items->item->{'submission-hints'}->attachments['upload'] == 'true') { + $this->options['file_upload'] = 1; + } + } + } +} diff --git a/lib/models/vips/VipsAssignment.php b/lib/models/vips/VipsAssignment.php new file mode 100644 index 0000000..d73d62a --- /dev/null +++ b/lib/models/vips/VipsAssignment.php @@ -0,0 +1,1308 @@ + VipsAssignmentAttempt::class, + 'assoc_foreign_key' => 'assignment_id' + ]; + $config['has_many']['solutions'] = [ + 'class_name' => VipsSolution::class, + 'assoc_foreign_key' => 'assignment_id' + ]; + + $config['belongs_to']['course'] = [ + 'class_name' => Course::class, + 'foreign_key' => 'range_id' + ]; + $config['belongs_to']['block'] = [ + 'class_name' => VipsBlock::class, + 'foreign_key' => 'block_id' + ]; + $config['belongs_to']['test'] = [ + 'class_name' => VipsTest::class, + 'foreign_key' => 'test_id' + ]; + + parent::configure($config); + } + + /** + * Initialize a new instance of this class. + */ + public function __construct($id = null) + { + parent::__construct($id); + + if (is_null($this->options)) { + $this->options = []; + } + } + + /** + * Delete entry from the database. + */ + public function delete() + { + $gradebook_id = $this->options['gradebook_id']; + + if ($gradebook_id) { + Grading\Definition::deleteBySQL('id = ?', [$gradebook_id]); + } + + VipsAssignmentAttempt::deleteBySQL('assignment_id = ?', [$this->id]); + + $ref_count = self::countBySql('test_id = ?', [$this->test_id]); + + if ($ref_count === 1) { + $this->test->delete(); + } + + return parent::delete(); + } + + /** + * Find all assignments for a given range_id. + * + * @return VipsAssignment[] + */ + public static function findByRangeId($range_id) + { + return VipsAssignment::findBySQL( + 'range_id = ? AND type IN (?) ORDER BY start', + [$range_id, ['exam', 'practice', 'selftest']] + ); + } + + public static function importText( + string $title, + string $string, + string $user_id, + string $course_id + ): VipsAssignment { + $duration = 7 * 24 * 60 * 60; // one week + + $data_test = [ + 'title' => $title !== '' ? $title : _('Aufgabenblatt'), + 'description' => '', + 'user_id' => $user_id + ]; + $data = [ + 'type' => 'practice', + 'range_id' => $course_id ?: $user_id, + 'range_type' => $course_id ? 'course' : 'user', + 'start' => strtotime(date('Y-m-d H:00:00')), + 'end' => strtotime(date('Y-m-d H:00:00', time() + $duration)) + ]; + + // remove comments + $string = preg_replace('/^#.*/m', '', $string); + + // split into exercises + $segments = preg_split('/^Name:/m', $string); + array_shift($segments); + + $test_obj = VipsTest::create($data_test); + + $result = self::build($data); + $result->test = $test_obj; + $result->store(); + + foreach ($segments as $segment) { + try { + $new_exercise = Exercise::importText($segment); + $new_exercise->user_id = $user_id; + $new_exercise->store(); + $test_obj->addExercise($new_exercise); + } catch (Exception $e) { + $errors[] = $e->getMessage(); + } + } + + if (isset($errors)) { + PageLayout::postError(_('Während des Imports sind folgende Fehler aufgetreten:'), $errors); + } + + return $result; + } + + public static function importXML( + string $string, + string $user_id, + string $course_id + ): VipsAssignment { + // default options + $options = [ + 'evaluation_mode' => 0, + 'released' => 0 + ]; + + $duration = 7 * 24 * 60 * 60; // one week + + $data_test = [ + 'title' => _('Aufgabenblatt'), + 'description' => '', + 'user_id' => $user_id + ]; + $data = [ + 'type' => 'practice', + 'range_id' => $course_id ?: $user_id, + 'range_type' => $course_id ? 'course' : 'user', + 'start' => strtotime(date('Y-m-d H:00:00')), + 'end' => strtotime(date('Y-m-d H:00:00', time() + $duration)), + 'options' => $options + ]; + + $test = new SimpleXMLElement($string, LIBXML_COMPACT | LIBXML_NOCDATA); + $data['type'] = (string) $test['type']; + + if (trim($test->title) !== '') { + $data_test['title'] = trim($test->title); + } + if ($test->description) { + $data_test['description'] = Studip\Markup::purifyHtml(trim($test->description)); + } + if ($test->notes) { + $data['options']['notes'] = trim($test->notes); + } + + if ($test->limit['access-code']) { + $data['options']['access_code'] = (string) $test->limit['access-code']; + } + if ($test->limit['ip-ranges']) { + $data['options']['ip_range'] = (string) $test->limit['ip-ranges']; + } + if ($test->limit['resets']) { + $data['options']['resets'] = (int) $test->limit['resets']; + } + if ($test->limit['tries']) { + $data['options']['max_tries'] = (int) $test->limit['tries']; + } + + if ($test->option['scoring-mode'] == 'negative_points') { + $data['options']['evaluation_mode'] = self::SCORING_NEGATIVE_POINTS; + } else if ($test->option['scoring-mode'] == 'all_or_nothing') { + $data['options']['evaluation_mode'] = self::SCORING_ALL_OR_NOTHING; + } + if ($test->option['shuffle-answers'] == 'true') { + $data['options']['shuffle_answers'] = 1; + } + if ($test->option['shuffle-exercises'] == 'true') { + $data['options']['shuffle_exercises'] = 1; + } + + if ($test['start']) { + $data['start'] = strtotime($test['start']); + } + if ($test['end']) { + $data['end'] = strtotime($test['end']); + } else if ($data['type'] === 'selftest') { + $data['end'] = null; + } + if ($test['duration']) { + $data['options']['duration'] = (int) $test['duration']; + } + if ($test['block'] && $course_id) { + $block = VipsBlock::findOneBySQL('name = ? AND range_id = ?', [$test['block'], $course_id]); + + if (!$block) { + $block = VipsBlock::create(['name' => $test['block'], 'range_id' => $course_id]); + } + + $data['block_id'] = $block->id; + } + + if ($test->{'feedback-items'}) { + foreach ($test->{'feedback-items'}->feedback as $feedback) { + $threshold = (int) ($feedback['score'] * 100); + $data['options']['feedback'][$threshold] = Studip\Markup::purifyHtml(trim($feedback)); + } + + krsort($data['options']['feedback']); + } + + $test_obj = VipsTest::create($data_test); + + $result = self::build($data); + $result->test = $test_obj; + $result->store(); + + if ($test->files) { + foreach ($test->files->file as $file) { + $file_id = (string) $file['id']; + $content = base64_decode((string) $file); + + $test->registerXPathNamespace('vips', 'urn:vips:test:v1.0'); + $file_refs = $test->xpath('vips:exercises/*/vips:file-refs/*[@ref="' . $file_id . '"]'); + + if ($file_refs && $content !== false) { + if (strlen($file_id) > 5 && str_starts_with($file_id, 'file-')) { + $vips_file = File::find(substr($file_id, 5)); + + // try to avoid reupload of identical files + if ($vips_file && sha1_file($vips_file->getPath()) === sha1($content)) { + $files[$file_id] = $vips_file; + continue; + } + } + + $file = File::create([ + 'user_id' => $user_id, + 'mime_type' => get_mime_type($file['name']), + 'name' => basename($file['name']), + 'size' => strlen($content) + ]); + + file_put_contents($file->getPath(), $content); + } + } + + if (isset($files)) { + $mapped = preg_replace_callback( + '/\burn:vips:file-ref:([A-Za-z_][\w.-]*)/', + function($match) use ($files) { + $file = $files[$match[1]]; + + if ($file) { + return htmlReady($file->getDownloadURL()); + } else { + return $match[0]; + } + }, $string + ); + $test = new SimpleXMLElement($mapped, LIBXML_COMPACT | LIBXML_NOCDATA); + } + } + + foreach ($test->exercises->exercise as $exercise) { + try { + $new_exercise = Exercise::importXML($exercise); + $new_exercise->user_id = $user_id; + $new_exercise->store(); + $exercise_ref = $test_obj->addExercise($new_exercise); + + if ($exercise['points']) { + $exercise_ref->points = (float) $exercise['points']; + $exercise_ref->store(); + } + + if ($exercise->{'file-refs'}) { + $folder = Folder::findTopFolder($new_exercise->id, 'ExerciseFolder', 'task'); + + foreach ($exercise->{'file-refs'}->{'file-ref'} as $file_ref) { + $file = $files[(string) $file_ref['ref']]; + + if ($file) { + FileRef::create([ + 'file_id' => $file->id, + 'folder_id' => $folder->id, + 'object_id' => $new_exercise->id, + 'user_id' => $user_id, + 'name' => $file->name + ]); + } + } + } + } catch (Exception $e) { + $errors[] = $e->getMessage(); + } + } + + if (isset($errors)) { + PageLayout::postError(_('Während des Imports sind folgende Fehler aufgetreten:'), $errors); + } + + return $result; + } + + /** + * Get the name of this assignment type. + */ + public function getTypeName(): string + { + $assignment_types = self::getAssignmentTypes(); + + return $assignment_types[$this->type]['name']; + } + + /** + * Get the icon of this assignment type. + */ + public function getTypeIcon(string $role = Icon::DEFAULT_ROLE): Icon + { + $assignment_types = self::getAssignmentTypes(); + + return Icon::create( + $assignment_types[$this->type]['icon'], + $role, + ['aria-hidden' => 'true', 'title' => $assignment_types[$this->type]['name']] + ); + } + + /** + * Get the list of supported assignment types. + */ + public static function getAssignmentTypes(): array + { + return [ + 'practice' => ['name' => _('Übung'), 'icon' => 'file'], + 'selftest' => ['name' => _('Selbsttest'), 'icon' => 'check-circle'], + 'exam' => ['name' => _('Klausur'), 'icon' => 'doctoral_cap'] + ]; + } + + /** + * Check if this assignment is locked for editing. + */ + public function isLocked(): bool + { + return $this->type === 'exam' && $this->countAssignmentAttempts() > 0; + } + + /** + * Check if this assignment is visible to this user. + */ + public function isVisible(string $user_id): bool + { + return $this->block_id ? $this->block->isVisible($user_id) : true; + } + + /** + * Check if this assignment has been started. + */ + public function isStarted(): bool + { + $now = time(); + + return $now >= $this->start; + } + + /** + * Check if this assignment is currently running. + * + * @param string|null $user_id check end time for this user id (optional) + */ + public function isRunning(?string $user_id = null): bool + { + $now = time(); + $end = $user_id ? $this->getUserEndTime($user_id) : $this->end; + + return $now >= $this->start && ($end === null || $now <= $end); + } + + /** + * Check if this assignment is already finished. + * + * @param string|null $user_id check end time for this user id (optional) + */ + public function isFinished(?string $user_id = null): bool + { + $now = time(); + $end = $user_id ? $this->getUserEndTime($user_id) : $this->end; + + return $end && $now > $end; + } + + /** + * Check if this assignment has no end date. + */ + public function isUnlimited(): bool + { + return $this->type === 'selftest' && $this->end === null; + } + + /** + * Check if this assignment may use self assessment features. + */ + public function isSelfAssessment(): bool + { + return $this->type === 'selftest' || $this->options['self_assessment']; + } + + /** + * Check if a user may reset and restart this assignment. + */ + public function isResetAllowed(): bool + { + return $this->isSelfAssessment() && $this->options['resets'] !== 0; + } + + /** + * Check if this assignment presents shuffled exercises. + */ + public function isExerciseShuffled(): bool + { + return $this->type === 'exam' && $this->options['shuffle_exercises']; + } + + /** + * Check if this assignment presents shuffled answers. + */ + public function isShuffled(): bool + { + return $this->type === 'exam' && $this->options['shuffle_answers'] !== 0; + } + + /** + * Check if this assignment is using group solutions. + */ + public function hasGroupSolutions(): bool + { + return $this->type === 'practice' && $this->options['use_groups'] !== 0; + } + + /** + * Get the number of tries allowed for exercises on this assignment. + */ + public function getMaxTries(): int + { + if ($this->type === 'selftest') { + return $this->options['max_tries'] ?? 3; + } + + return 0; + } + + /** + * Check whether the given exercise is part of this assignment. + * + * @param int $exercise_id exercise id + */ + public function hasExercise(int $exercise_id): bool + { + return VipsExerciseRef::exists([$this->test_id, $exercise_id]); + } + + /** + * Return array of exercise refs in the test of this assignment. + */ + public function getExerciseRefs(?string $user_id): array + { + $result = $this->test->exercise_refs->getArrayCopy(); + + if ($this->isExerciseShuffled() && $user_id) { + srand(crc32($this->id . ':' . $user_id)); + shuffle($result); + srand(); + } + + return $result; + } + + /** + * Export this assignment to XML format. Returns the XML string. + */ + public function exportXML(): string + { + $files = []; + + foreach ($this->test->exercise_refs as $exercise_ref) { + $exercise = $exercise_ref->exercise; + $exercise->includeFilesForExport(); + + if ($exercise->folder) { + foreach ($exercise->folder->file_refs as $file_ref) { + $files[$file_ref->file_id] = $file_ref->file; + } + } + } + + $template = VipsModule::$template_factory->open('sheets/export_assignment'); + $template->assignment = $this; + $template->files = $files; + + // delete all characters outside the valid character range for XML + // documents (#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]). + return preg_replace("/[^\t\n\r -\xFF]/", '', $template->render()); + } + + /** + * Check whether this assignment is editable by the given user. + * + * @param string|null $user_id user to check (defaults to current user) + */ + public function checkEditPermission(?string $user_id = null): bool + { + if ($this->range_type === 'user') { + return $this->range_id === ($user_id ?: $GLOBALS['user']->id); + } + + return $GLOBALS['perm']->have_studip_perm('tutor', $this->range_id, $user_id); + } + + /** + * Check whether this assignment is viewable by the given user. + * + * @param string|null $user_id user to check (defaults to current user) + */ + public function checkViewPermission(?string $user_id = null): bool + { + if ($this->range_type === 'user') { + return $this->range_id === ($user_id ?: $GLOBALS['user']->id); + } + + return $GLOBALS['perm']->have_studip_perm('autor', $this->range_id, $user_id); + } + + /** + * Check whether this assignment is accessible to a student. This is just + * a shortcut for checking: running, active, ip address and access code. + * + * @param string $user_id check end time for this user id (optional) + */ + public function checkAccess($user_id = null): bool + { + return $this->isRunning($user_id) + && $this->active && $this->checkAccessCode() + && $this->checkIPAccess($_SERVER['REMOTE_ADDR']); + } + + /** + * Check whether the access code provided for this assignment is valid. + * If $access_code is null, the code stored in the user session is used. + * + * @param string|null $access_code access code (optional) + */ + public function checkAccessCode(?string $access_code = null): bool + { + if (isset($access_code)) { + $_SESSION['vips_access_' . $this->id] = $access_code; + } else if (isset($_SESSION['vips_access_' . $this->id])) { + $access_code = $_SESSION['vips_access_' . $this->id]; + } else { + $access_code = null; + } + + return in_array($this->options['access_code'], [null, $access_code], true); + } + + /** + * Check whether the given IP address listed among the IP addresses given + * by the lecturer for this exam (if applicable). + * + * @param string $ip_addr IPv4 or IPv6 address + */ + public function checkIPAccess(string $ip_addr): bool + { + // not an exam: user has access. + if ($this->type !== 'exam') { + return true; + } + + $ip_addr = inet_pton($ip_addr); + $ip_ranges = $this->options['ip_range']; + $exam_rooms = Config::get()->VIPS_EXAM_ROOMS; + + // expand exam room names + if ($exam_rooms) { + $ip_ranges = preg_replace_callback('/#([^ ,]+)/', + function($match) use ($exam_rooms) { + return $exam_rooms[$match[1]]; + }, $ip_ranges); + } + + // Explode space separated list into an array and check the resulting single IPs + $ip_ranges = preg_split('/[ ,]+/', $ip_ranges, -1, PREG_SPLIT_NO_EMPTY); + + // No IP given: user has access. + if (count($ip_ranges) == 0) { + return true; + } + + // One or more IPs are given and user IP matches at least one: user has access. + foreach ($ip_ranges as $ip_range) { + if (str_contains($ip_range, '/')) { + [$ip_range, $bits] = explode('/', $ip_range); + $ip_range = inet_pton($ip_range) ?: ''; + $mask = str_repeat(chr(0), strlen($ip_range)); + + for ($i = 0; $i < strlen($mask); ++$i) { + if ($bits >= 8) { + $bits -= 8; + } else { + $mask[$i] = chr((1 << 8 - $bits) - 1); + $bits = 0; + } + } + + $ip_start = $ip_range & ~$mask; + $ip_end = $ip_range | $mask; + } else { + if (str_contains($ip_range, '-')) { + [$ip_start, $ip_end] = explode('-', $ip_range); + } else { + $ip_start = $ip_end = $ip_range; + } + + if (!str_contains($ip_range, ':')) { + $ip_start = implode('.', array_pad(explode('.', $ip_start), 4, 0)); + $ip_end = implode('.', array_pad(explode('.', $ip_end), 4, 255)); + } + + $ip_start = inet_pton($ip_start); + $ip_end = inet_pton($ip_end); + } + + if (strcmp($ip_start, $ip_addr) <= 0 && strcmp($ip_addr, $ip_end) <= 0) { + return true; + } + } + + return false; + } + + /** + * Get the release status of this assignment for the given user. + * + * Valid values are: + * - 0 = not released + * - 1 = points + * - 2 = comments + * - 3 = corrections + * - 4 = sample solutions + * + * See the according constants of this class. + */ + public function releaseStatus(string $user_id): int + { + if ($this->isFinished() || $this->isSelfAssessment() && $this->isFinished($user_id)) { + if ($this->type === 'exam') { + if ($this->getAssignmentAttempt($user_id)) { + return $this->options['released'] ?? self::RELEASE_STATUS_NONE; + } + } else { + if ($this->options['released'] > 0) { + return $this->options['released']; + } + } + } + + return self::RELEASE_STATUS_NONE; + } + + /** + * Count the number of assignment attempts for this assignment. + */ + public function countAssignmentAttempts(): int + { + return VipsAssignmentAttempt::countBySql('assignment_id = ?', [$this->id]); + } + + /** + * Get the assignment attempt of the given user for this assignment. + * Returns null if there is no assignment attempt for this user. + * + * @param string $user_id user id + */ + public function getAssignmentAttempt(string $user_id): ?VipsAssignmentAttempt + { + return VipsAssignmentAttempt::findOneBySQL('assignment_id = ? AND user_id = ?', [$this->id, $user_id]); + } + + /** + * Record an assignment attempt for the given user for this assignment. + */ + public function recordAssignmentAttempt(string $user_id): void + { + if (!$this->getAssignmentAttempt($user_id)) { + if ($this->type === 'exam') { + $end = time() + $this->options['duration'] * 60; + $ip_address = $_SERVER['REMOTE_ADDR']; + $options = ['session_id' => session_id()]; + } else { + $end = null; + $ip_address = ''; + $options = null; + } + + VipsAssignmentAttempt::create([ + 'assignment_id' => $this->id, + 'user_id' => $user_id, + 'start' => time(), + 'end' => $end, + 'ip_address' => $ip_address, + 'options' => $options + ]); + } + } + + /** + * Finish an assignment attempt for the given user for this assignment. + */ + public function finishAssignmentAttempt(string $user_id): ?VipsAssignmentAttempt + { + $assignment_attempt = $this->getAssignmentAttempt($user_id); + $now = time(); + + if ($assignment_attempt) { + if ($assignment_attempt->end === null || $assignment_attempt->end > $now) { + $assignment_attempt->end = $now; + $assignment_attempt->store(); + } + } + + return $assignment_attempt; + } + + /** + * Get the individual end time of the given user for this assignment. + */ + public function getUserEndTime(string $user_id): ?int + { + if ($this->type === 'practice') { + return $this->end; + } + + $assignment_attempt = $this->getAssignmentAttempt($user_id); + + if ($assignment_attempt) { + $start = $assignment_attempt->start; + } else { + $start = time(); + } + + if ($assignment_attempt && $assignment_attempt->end) { + return min($assignment_attempt->end, $this->end ?: $assignment_attempt->end); + } else if ($this->type === 'exam') { + return min($start + $this->options['duration'] * 60, $this->end); + } else { + return $this->end; + } + } + + /** + * Get all members that were assigned to a particular group for + * this assignment. + * + * @param VipsGroup $group The group object + * @return VipsGroupMember[] + */ + public function getGroupMembers($group): array + { + return VipsGroupMember::findBySQL( + 'group_id = ? AND start < ? AND (end > ? OR end IS NULL)', + [$group->id, $this->end, $this->end] + ); + } + + /** + * Get the group the user was assigned to for this assignment. + * Returns null if there is no group assignment for this user. + */ + public function getUserGroup(string $user_id): ?VipsGroup + { + if (!$this->hasGroupSolutions()) { + return null; + } + + return VipsGroup::findOneBySQL( + 'JOIN etask_group_members ON group_id = statusgruppe_id + WHERE range_id = ? + AND user_id = ? + AND start < ? + AND (end > ? OR end IS NULL)', + [$this->range_id, $user_id, $this->end, $this->end] + ); + } + + /** + * Store a solution related to this assignment into the database. + * + * @param VipsSolution $solution The solution object + */ + public function storeSolution(VipsSolution $solution): bool|int + { + $solution->assignment = $this; + + // store some client info for exams + if ($this->type === 'exam') { + $solution->ip_address = $_SERVER['REMOTE_ADDR']; + $solution->options['session_id'] = session_id(); + } + + // in selftests, autocorrect solution + if ($this->isSelfAssessment()) { + $this->correctSolution($solution); + } + + // insert new solution into etask_responses + return $solution->store(); + } + + /** + * Correct a solution and store the points for the solution in the object. + * + * @param VipsSolution $solution The solution object + * @param bool $corrected mark solution as corrected + */ + public function correctSolution(VipsSolution $solution, bool $corrected = false): void + { + $exercise = $solution->exercise; + $exercise_ref = $this->test->getExerciseRef($exercise->id); + $max_points = (float) $exercise_ref->points; + + // always set corrected to true for selftest exercises + $selftest = $this->type === 'selftest'; + $evaluation = $exercise->evaluate($solution); + $eval_safe = $selftest ? $evaluation['safe'] !== null : $evaluation['safe']; + + $reached_points = round($evaluation['percent'] * $max_points * 2) / 2; + $corrected = (int) ($corrected || $eval_safe); + + // insert solution points + $solution->state = $corrected; + $solution->points = $reached_points; + $solution->chdate = time(); + + if ($selftest && $evaluation['percent'] != 1 && isset($exercise->options['feedback'])) { + $solution->feedback = $exercise->options['feedback']; + } + } + + /** + * Restores an archived solution as the current solution. + * + * @param VipsSolution $solution The solution object + */ + public function restoreSolution(VipsSolution $solution): void + { + if ($solution->isArchived() && $solution->assignment_id == $this->id) { + $new_solution = VipsSolution::build($solution); + $new_solution->id = 0; + + if ($solution->folder) { + $new_solution->store(); + $folder = Folder::findTopFolder($new_solution->id, 'ResponseFolder', 'response'); + + foreach ($solution->folder->file_refs as $file_ref) { + FileManager::copyFile($file_ref->getFileType(), $folder->getTypedFolder(), $file_ref->user); + } + } + + $this->storeSolution($new_solution); + } + } + + /** + * Fetch archived solutions related to this assignment from the database. + * Returns empty list if there are no archived solutions for this exercise. + * + * @return VipsSolution[] + */ + public function getArchivedGroupSolutions(string $group_id, int $exercise_id): array + { + return VipsSolution::findBySQL( + 'JOIN etask_group_members USING(user_id) + WHERE task_id = ? + AND assignment_id = ? + AND group_id = ? + AND start < ? + AND (end > ? OR end IS NULL) + ORDER BY mkdate DESC', + [$exercise_id, $this->id, $group_id, $this->end, $this->end] + ); + } + + /** + * Fetch archived solutions related to this assignment from the database. + * NOTE: This method will NOT check the group solutions, if applicable. + * Returns empty list if there are no archived solutions for this exercise. + * + * @return VipsSolution[] + */ + public function getArchivedUserSolutions(string $user_id, int $exercise_id): array + { + return VipsSolution::findBySQL( + 'task_id = ? AND assignment_id = ? AND user_id = ? ORDER BY mkdate DESC', + [$exercise_id, $this->id, $user_id] + ); + } + + /** + * Fetch archived solutions related to this assignment from the database. + * Returns empty list if there are no archived solutions for this exercise. + * + * @return VipsSolution[] + */ + public function getArchivedSolutions(string $user_id, int $exercise_id): array + { + $group = $this->getUserGroup($user_id); + + if ($group) { + return $this->getArchivedGroupSolutions($group->id, $exercise_id); + } + + return $this->getArchivedUserSolutions($user_id, $exercise_id); + } + + /** + * Fetch a solution related to this assignment from the database. + * Returns null if there is no solution for this exercise yet. + */ + public function getGroupSolution(string $group_id, int $exercise_id): ?VipsSolution + { + return VipsSolution::findOneBySQL( + 'JOIN etask_group_members USING(user_id) + WHERE task_id = ? + AND assignment_id = ? + AND group_id = ? + AND start < ? + AND (end > ? OR end IS NULL) + ORDER BY mkdate DESC', + [$exercise_id, $this->id, $group_id, $this->end, $this->end] + ); + } + + /** + * Fetch a solution related to this assignment from the database. + * NOTE: This method will NOT check the group solution, if applicable. + * Returns null if there is no solution for this exercise yet. + */ + public function getUserSolution(string $user_id, int $exercise_id): ?VipsSolution + { + return VipsSolution::findOneBySQL( + 'task_id = ? AND assignment_id = ? AND user_id = ? ORDER BY mkdate DESC', + [$exercise_id, $this->id, $user_id] + ); + } + + /** + * Fetch a solution related to this assignment from the database. + * Returns null if there is no solution for this exercise yet. + */ + public function getSolution(string $user_id, int $exercise_id): ?VipsSolution + { + $group = $this->getUserGroup($user_id); + + if ($group) { + return $this->getGroupSolution($group->id, $exercise_id); + } + + return $this->getUserSolution($user_id, $exercise_id); + } + + /** + * Delete all solutions of the given user for a single exercise of + * this test from the DB. + */ + public function deleteSolution(string $user_id, int $exercise_id): void + { + $sql = 'task_id = ? AND assignment_id = ? AND user_id = ?'; + + if ($this->isSelfAssessment()) { + // delete in etask_responses + VipsSolution::deleteBySQL($sql, [$exercise_id, $this->id, $user_id]); + } + + // update gradebook if necessary + $this->updateGradebookEntries($user_id); + } + + /** + * Delete all solutions of the given user for this test from the DB. + */ + public function deleteSolutions(string $user_id): void + { + $sql = 'assignment_id = ? AND user_id = ?'; + + if ($this->isSelfAssessment()) { + // delete in etask_responses + VipsSolution::deleteBySQL($sql, [$this->id, $user_id]); + } + + // delete start times + VipsAssignmentAttempt::deleteBySQL($sql, [$this->id, $user_id]); + + // update gradebook if necessary + $this->updateGradebookEntries($user_id); + } + + /** + * Delete all solutions of all users for this test from the DB. + */ + public function deleteAllSolutions(): void + { + $sql = 'assignment_id = ?'; + + if ($this->isSelfAssessment()) { + // delete in etask_responses + VipsSolution::deleteBySQL($sql, [$this->id]); + } + + // delete start times + VipsAssignmentAttempt::deleteBySQL($sql, [$this->id]); + + // update gradebook if necessary + $this->updateGradebookEntries(); + } + + /** + * Count the number of solutions of the given user for this test. + */ + public function countSolutions(string $user_id): int + { + $solutions = 0; + + foreach ($this->test->exercise_refs as $exercise_ref) { + if ($this->getSolution($user_id, $exercise_ref->task_id)) { + ++$solutions; + } + } + + return $solutions; + } + + /** + * Return the points a user has reached in all exercises in this assignment. + */ + public function getUserPoints(string $user_id): float|int + { + $group = $this->getUserGroup($user_id); + + if ($group) { + $user_ids = array_column($this->getGroupMembers($group), 'user_id'); + } else { + $user_ids = [$user_id]; + } + + $solutions = $this->solutions->findBy('user_id', $user_ids)->orderBy('mkdate'); + $points = []; + + foreach ($solutions as $solution) { + $points[$solution->task_id] = (float) $solution->points; + } + + return max(array_sum($points), 0); + } + + /** + * Return the progress a user has achieved on this assignment (range 0..1). + */ + public function getUserProgress(string $user_id): float|int + { + $group = $this->getUserGroup($user_id); + $max_points = 0; + $progress = 0; + + foreach ($this->test->exercise_refs as $exercise_ref) { + $max_points += $exercise_ref->points; + + if ($group) { + $solution = $this->getGroupSolution($group->id, $exercise_ref->task_id); + } else { + $solution = $this->getUserSolution($user_id, $exercise_ref->task_id); + } + + if ($solution) { + $progress += $exercise_ref->points; + } + } + + return $max_points ? $progress / $max_points : 0; + } + + /** + * Return the individual feedback text for the given user in this assignment. + */ + public function getUserFeedback(string $user_id): ?string + { + if (isset($this->options['feedback'])) { + $user_points = $this->getUserPoints($user_id); + $max_points = $this->test->getTotalPoints(); + $percent = $user_points / $max_points * 100; + + foreach ($this->options['feedback'] as $threshold => $feedback) { + if ($percent >= $threshold) { + return $feedback; + } + } + } + + return null; + } + + /** + * Copy this assignment into the given course. Returns the new assignment. + */ + public function copyIntoCourse(string $course_id, string $range_type = 'course'): ?VipsAssignment + { + // determine title of new assignment + if ($this->range_id === $course_id) { + $title = sprintf(_('Kopie von %s'), $this->test->title); + } else { + $title = $this->test->title; + } + + // reset released option for new assignment + $options = $this->options; + unset($options['released']); + unset($options['stopdate']); + unset($options['gradebook_id']); + + $new_test = VipsTest::create([ + 'title' => $title, + 'description' => $this->test->description, + 'user_id' => $GLOBALS['user']->id + ]); + + $new_assignment = VipsAssignment::create([ + 'test_id' => $new_test->id, + 'range_id' => $course_id, + 'range_type' => $range_type, + 'type' => $this->type, + 'start' => $this->start, + 'end' => $this->end, + 'options' => $options + ]); + + foreach ($this->test->exercise_refs as $exercise_ref) { + $exercise_ref->copyIntoTest($new_test->id, $exercise_ref->position); + } + + return $new_assignment; + } + + /** + * Move this assignment into the given course. + */ + public function moveIntoCourse(string $course_id, string $range_type = 'course'): void + { + if ($this->range_id !== $course_id) { + $this->range_id = $course_id; + $this->range_type = $range_type; + $this->block_id = null; + $this->removeFromGradebook(); + $this->store(); + } + } + + /** + * Insert this assignment into the gradebook of its course. + * + * @param string $title gradebook title + * @param float $weight gradebook weight + */ + public function insertIntoGradebook(string $title, float $weight = 1): void + { + $gradebook_id = $this->options['gradebook_id']; + + if (!$gradebook_id) { + $definition = Grading\Definition::create([ + 'course_id' => $this->range_id, + 'item' => $this->id, + 'name' => $title, + 'tool' => _('Aufgaben'), + 'category' => $this->getTypeName(), + 'position' => $this->start, + 'weight' => $weight + ]); + + $this->options['gradebook_id'] = $definition->id; + $this->store(); + } + } + + /** + * Remove this assignment from the gradebook of its course. + */ + public function removeFromGradebook(): void + { + $gradebook_id = $this->options['gradebook_id']; + + if ($gradebook_id) { + Grading\Definition::find($gradebook_id)->delete(); + + unset($this->options['gradebook_id']); + $this->store(); + } + } + + /** + * Update some or all gradebook entries of this assignment. If the + * user_id is specified, only update entries related to this user. + * + * @param string|null $user_id user id + */ + public function updateGradebookEntries(?string $user_id = null): void + { + $gradebook_id = $this->options['gradebook_id']; + + if ($gradebook_id) { + $max_points = $this->test->getTotalPoints() ?: 1; + + if ($user_id) { + $group = $this->getUserGroup($user_id); + } + + if ($group) { + $members = $this->getGroupMembers($group); + } else if ($user_id) { + $members = [(object) compact('user_id')]; + } else { + $members = $this->course->members->findBy('status', 'autor'); + } + + foreach ($members as $member) { + $reached_points = $this->getUserPoints($member->user_id); + $entry = new Grading\Instance([$gradebook_id, $member->user_id]); + + if ($reached_points) { + $entry->rawgrade = $reached_points / $max_points; + $entry->store(); + } else { + $entry->delete(); + } + } + } + } +} diff --git a/lib/models/vips/VipsAssignmentAttempt.php b/lib/models/vips/VipsAssignmentAttempt.php new file mode 100644 index 0000000..9eba371 --- /dev/null +++ b/lib/models/vips/VipsAssignmentAttempt.php @@ -0,0 +1,99 @@ + VipsAssignment::class, + 'foreign_key' => 'assignment_id' + ]; + $config['belongs_to']['user'] = [ + 'class_name' => User::class, + 'foreign_key' => 'user_id' + ]; + + parent::configure($config); + } + + /** + * Return a student's event log for the assignment as a data array. + */ + public function getLogEntries(): array + { + $assignment = $this->assignment; + $user_id = $this->user_id; + $end_time = min($this->end, $assignment->end); + + $solutions = VipsSolution::findBySQL('assignment_id = ? AND user_id = ?', [$assignment->id, $user_id]); + + foreach ($assignment->test->exercise_refs as $exercise_ref) { + $position[$exercise_ref->task_id] = $exercise_ref->position; + } + + $logs[] = [ + 'label' => _('Beginn der Klausur'), + 'time' => $this->start, + 'ip_address' => $this->ip_address, + 'session_id' => $this->options['session_id'], + 'archived' => false + ]; + + foreach ($solutions as $solution) { + if ($solution->isSubmitted()) { + $logs[] = [ + 'label' => sprintf(_('Abgabe Aufgabe %d'), $position[$solution->task_id]), + 'time' => $solution->mkdate, + 'ip_address' => $solution->ip_address, + 'session_id' => $solution->options['session_id'], + 'archived' => $solution->isArchived(), + ]; + } + } + + if ($end_time && $end_time < date('Y-m-d H:i:s')) { + $logs[] = [ + 'label' => _('Ende der Klausur'), + 'time' => $end_time, + 'ip_address' => '', + 'session_id' => '', + 'archived' => false + ]; + } + + usort($logs, fn($a, $b) => $a['time'] <=> $b['time']); + + return $logs; + } +} diff --git a/lib/models/vips/VipsBlock.php b/lib/models/vips/VipsBlock.php new file mode 100644 index 0000000..2179254 --- /dev/null +++ b/lib/models/vips/VipsBlock.php @@ -0,0 +1,92 @@ + VipsAssignment::class, + 'assoc_foreign_key' => 'block_id' + ]; + + $config['belongs_to']['course'] = [ + 'class_name' => Course::class, + 'foreign_key' => 'range_id' + ]; + $config['belongs_to']['group'] = [ + 'class_name' => Statusgruppen::class, + 'foreign_key' => 'group_id' + ]; + + parent::configure($config); + } + + /** + * Delete entry from the database. + */ + public function delete() + { + foreach ($this->assignments as $assignment) { + $assignment->block_id = null; + $assignment->store(); + } + + return parent::delete(); + } + + /** + * Check if this block is visible to this user. + */ + public function isVisible(string $user_id): bool + { + $visible = $this->visible; + + if ($visible && $this->group_id) { + $visible = StatusgruppeUser::exists([$this->group_id, $user_id]); + } + + return $visible; + } + + /** + * Get the first assignment attempt of the given user for this block. + * Returns null if there is no assignment attempt for this user. + * + * @param string $user_id user id + */ + public function getAssignmentAttempt(string $user_id): ?VipsAssignmentAttempt + { + $assignment_ids = $this->assignments->pluck('id'); + + return VipsAssignmentAttempt::findOneBySQL( + 'assignment_id IN (?) AND user_id = ? ORDER BY start', [$assignment_ids, $user_id] + ); + } +} diff --git a/lib/models/vips/VipsExerciseRef.php b/lib/models/vips/VipsExerciseRef.php new file mode 100644 index 0000000..3255e27 --- /dev/null +++ b/lib/models/vips/VipsExerciseRef.php @@ -0,0 +1,137 @@ + Exercise::class, + 'foreign_key' => 'task_id' + ]; + $config['belongs_to']['test'] = [ + 'class_name' => VipsTest::class, + 'foreign_key' => 'test_id' + ]; + + parent::configure($config); + } + + /** + * Set value for the "exercise" relation (to avoid SORM errors). + */ + public function setExercise(Exercise $exercise): void + { + $this->task_id = $exercise->id; + $this->relations['exercise'] = $exercise; + } + + /** + * Delete entry from the database. + */ + public function delete() + { + $ref_count = self::countBySql('task_id = ?', [$this->task_id]); + + if ($ref_count == 1) { + $this->exercise->delete(); + } + + return parent::delete(); + } + + /** + * Copy the referenced exercise into the given test at the specified + * position (or at the end). Returns the new exercise reference. + * + * @param string $test_id test id + * @param int $position exercise position (optional) + */ + public function copyIntoTest(string $test_id, ?int $position = null): VipsExerciseRef + { + $db = DBManager::get(); + + if ($position === null) { + $stmt = $db->prepare('SELECT MAX(position) FROM etask_test_tasks WHERE test_id = ?'); + $stmt->execute([$test_id]); + $position = $stmt->fetchColumn() + 1; + } + + $new_exercise = Exercise::create([ + 'type' => $this->exercise->type, + 'title' => $this->exercise->title, + 'description' => $this->exercise->description, + 'task' => $this->exercise->task, + 'options' => $this->exercise->options, + 'user_id' => $GLOBALS['user']->id + ]); + + if ($this->exercise->folder) { + $folder = Folder::findTopFolder($new_exercise->id, 'ExerciseFolder', 'task'); + + foreach ($this->exercise->folder->file_refs as $file_ref) { + FileManager::copyFile($file_ref->getFileType(), $folder->getTypedFolder(), User::findCurrent()); + } + } + + return VipsExerciseRef::create([ + 'task_id' => $new_exercise->id, + 'test_id' => $test_id, + 'points' => $this->points, + 'position' => $position + ]); + } + + /** + * Move the referenced exercise into the given test (at the end). + * + * @param string $test_id test id + */ + public function moveIntoTest(string $test_id): void + { + $db = DBManager::get(); + $old_test_id = $this->test_id; + $old_position = $this->position; + + if ($old_test_id != $test_id) { + $stmt = $db->prepare('SELECT MAX(position) FROM etask_test_tasks WHERE test_id = ?'); + $stmt->execute([$test_id]); + $this->position = $stmt->fetchColumn() + 1; + $this->test_id = $test_id; + $this->store(); + + // renumber following exercises + $sql = 'UPDATE etask_test_tasks SET position = position - 1 WHERE test_id = ? AND position > ?'; + $stmt = $db->prepare($sql); + $stmt->execute([$old_test_id, $old_position]); + } + } +} diff --git a/lib/models/vips/VipsGroup.php b/lib/models/vips/VipsGroup.php new file mode 100644 index 0000000..8b43c2e --- /dev/null +++ b/lib/models/vips/VipsGroup.php @@ -0,0 +1,79 @@ + VipsGroupMember::class, + 'assoc_foreign_key' => 'group_id', + 'on_delete' => 'delete' + ]; + $config['has_many']['current_members'] = [ + 'class_name' => VipsGroupMember::class, + 'assoc_foreign_key' => 'group_id', + 'order_by' => 'AND end IS NULL' + ]; + + $config['belongs_to']['course'] = [ + 'class_name' => Course::class, + 'foreign_key' => 'range_id' + ]; + + parent::configure($config); + } + + /** + * Get the group the user is currently assigned to in a course. + * Returns null if there is no group assignment for this user. + * + * @param string $user_id user id + * @param string $course_id course id + */ + public static function getUserGroup(string $user_id, string $course_id): ?VipsGroup + { + return self::findOneBySQL( + 'JOIN etask_group_members ON group_id = statusgruppe_id + WHERE range_id = ? + AND user_id = ? + AND end IS NULL', + [$course_id, $user_id] + ); + } +} diff --git a/lib/models/vips/VipsGroupMember.php b/lib/models/vips/VipsGroupMember.php new file mode 100644 index 0000000..c6be629 --- /dev/null +++ b/lib/models/vips/VipsGroupMember.php @@ -0,0 +1,50 @@ + VipsGroup::class, + 'foreign_key' => 'group_id' + ]; + $config['belongs_to']['user'] = [ + 'class_name' => User::class, + 'foreign_key' => 'user_id' + ]; + + parent::configure($config); + } +} diff --git a/lib/models/vips/VipsSolution.php b/lib/models/vips/VipsSolution.php new file mode 100644 index 0000000..14b9826 --- /dev/null +++ b/lib/models/vips/VipsSolution.php @@ -0,0 +1,160 @@ + Folder::class, + 'assoc_foreign_key' => 'range_id', + 'assoc_func' => 'findByRangeIdAndFolderType', + 'foreign_key' => fn($record) => [$record->getId(), 'ResponseFolder'], + 'on_delete' => 'delete' + ]; + $config['has_one']['feedback_folder'] = [ + 'class_name' => Folder::class, + 'assoc_foreign_key' => 'range_id', + 'assoc_func' => 'findByRangeIdAndFolderType', + 'foreign_key' => fn($record) => [$record->getId(), 'FeedbackFolder'], + 'on_delete' => 'delete' + ]; + + $config['belongs_to']['exercise'] = [ + 'class_name' => Exercise::class, + 'foreign_key' => 'task_id' + ]; + $config['belongs_to']['assignment'] = [ + 'class_name' => VipsAssignment::class, + 'foreign_key' => 'assignment_id' + ]; + $config['belongs_to']['user'] = [ + 'class_name' => User::class, + 'foreign_key' => 'user_id' + ]; + + parent::configure($config); + } + + /** + * Update the gradebook entry. + */ + public function after_store(): void + { + $this->assignment->updateGradebookEntries($this->user_id); + } + + /** + * Set value for the "exercise" relation (to avoid SORM errors). + */ + public function setExercise(Exercise $exercise): void + { + $this->task_id = $exercise->id; + $this->relations['exercise'] = $exercise; + } + + /** + * Get array of submitted answers for this solution (PHP array). + */ + public function getResponse(): array + { + return $this->content['response']->getArrayCopy(); + } + + /** + * Check if this solution is archived. + */ + public function isArchived(): bool + { + $solution = VipsSolution::findOneBySql( + 'task_id = ? AND assignment_id = ? AND user_id = ? ORDER BY id DESC', + [$this->task_id, $this->assignment_id, $this->user_id] + ); + + return $solution && $this->id != $solution->id; + } + + /** + * Check if this solution is empty (default response and no files). + */ + public function isEmpty(): bool + { + return $this->response == $this->exercise->defaultResponse() + && $this->student_comment == '' + && (!$this->folder || count($this->folder->file_refs) === 0); + } + + /** + * Check if this solution has been submitted (is not a dummy solution). + */ + public function isSubmitted(): bool + { + return $this->id && !$this->mkdate; + } + + /** + * Check if this solution has any corrector feedback (text or files). + */ + public function hasFeedback() + { + return $this->feedback + || ($this->feedback_folder && count($this->feedback_folder->file_refs) > 0); + } + + /** + * Return the total number of solutions (including archived ones) + * submitted by the same user for this exercise. + */ + public function countTries(): int + { + if ($this->isNew()) { + return 0; + } + + return VipsSolution::countBySql( + 'task_id = ? AND assignment_id = ? AND user_id = ?', + [$this->task_id, $this->assignment_id, $this->user_id] + ); + } +} diff --git a/lib/models/vips/VipsTest.php b/lib/models/vips/VipsTest.php new file mode 100644 index 0000000..178b352 --- /dev/null +++ b/lib/models/vips/VipsTest.php @@ -0,0 +1,121 @@ + Exercise::class, + 'assoc_foreign_key' => 'id', + 'thru_table' => 'etask_test_tasks', + 'thru_key' => 'test_id', + 'thru_assoc_key' => 'task_id', + 'order_by' => 'ORDER BY position' + ]; + + $config['has_many']['assignments'] = [ + 'class_name' => VipsAssignment::class, + 'assoc_foreign_key' => 'test_id' + ]; + $config['has_many']['exercise_refs'] = [ + 'class_name' => VipsExerciseRef::class, + 'assoc_foreign_key' => 'test_id', + 'on_delete' => 'delete', + 'order_by' => 'ORDER BY position' + ]; + + $config['belongs_to']['user'] = [ + 'class_name' => User::class, + 'foreign_key' => 'user_id' + ]; + + parent::configure($config); + } + + public function addExercise(Exercise $exercise): VipsExerciseRef + { + $attributes = [ + 'task_id' => $exercise->id, + 'test_id' => $this->id, + 'position' => count($this->exercise_refs) + 1, + 'points' => $exercise->itemCount() + ]; + + $exercise_ref = VipsExerciseRef::create($attributes); + + $this->resetRelation('exercises'); + $this->resetRelation('exercise_refs'); + + return $exercise_ref; + } + + public function removeExercise(int $exercise_id): void + { + $db = DBManager::get(); + + $exercise_ref = VipsExerciseRef::find([$this->id, $exercise_id]); + $position = $exercise_ref->position; + + if ($exercise_ref->delete()) { + // renumber following exercises + $sql = 'UPDATE etask_test_tasks SET position = position - 1 WHERE test_id = ? AND position > ?'; + $stmt = $db->prepare($sql); + $stmt->execute([$this->id, $position]); + } + + $this->resetRelation('exercises'); + $this->resetRelation('exercise_refs'); + } + + public function getExerciseRef(int $exercise_id): ?VipsExerciseRef + { + return $this->exercise_refs->findOneBy('task_id', $exercise_id); + } + + /** + * Return the maximum number of points a person can get on this test. + * + * @return integer number of maximum points + */ + public function getTotalPoints(): int + { + $points = 0; + + foreach ($this->exercise_refs as $exercise_ref) { + $points += $exercise_ref->points; + } + + return $points; + } +} diff --git a/lib/modules/VipsModule.php b/lib/modules/VipsModule.php new file mode 100644 index 0000000..9c37c3a --- /dev/null +++ b/lib/modules/VipsModule.php @@ -0,0 +1,471 @@ +have_perm('root')) { + $nav_item = new Navigation(_('Klausuren'), 'dispatch.php/vips/config'); + Navigation::addItem('/admin/config/vips', $nav_item); + } + + if (Navigation::hasItem('/contents')) { + $nav_item = new Navigation(_('Aufgaben')); + $nav_item->setImage(Icon::create('vips')); + $nav_item->setDescription(_('Erstellen und Verwalten von Aufgabenblättern')); + Navigation::addItem('/contents/vips', $nav_item); + + $sub_item = new Navigation(_('Aufgabenblätter'), 'dispatch.php/vips/pool/assignments'); + $nav_item->addSubNavigation('assignments', $sub_item); + + $sub_item = new Navigation(_('Aufgaben'), 'dispatch.php/vips/pool/exercises'); + $nav_item->addSubNavigation('exercises', $sub_item); + } + + // check for running exams + if (Config::get()->VIPS_EXAM_RESTRICTIONS && !isset(self::$exam_mode)) { + $courses = self::getCoursesWithRunningExams($user->id); + self::$exam_mode = count($courses) > 0; + + if (self::$exam_mode) { + $page = basename($_SERVER['PHP_SELF']); + $path_info = Request::pathInfo(); + $course_id = Context::getId(); + + // redirect page calls if necessary + if (match_route('dispatch.php/jsupdater/get')) { + // always allow jsupdater calls + UpdateInformation::setInformation('vips', ['exam_mode' => true]); + } else if (isset($course_id, $courses[$course_id])) { + // course with running exam is selected, allow all exam actions + if (!match_route('dispatch.php/vips/sheets')) { + header('Location: ' . URLHelper::getURL('dispatch.php/vips/sheets')); + sess()->save(); + die(); + } + } else if (count($courses) === 1) { + // only one course with running exam, redirect there + header('Location: ' . URLHelper::getURL('dispatch.php/vips/sheets', ['cid' => key($courses)])); + sess()->save(); + + die(); + } else if (!match_route('dispatch.php/vips/exam_mode')) { + // forward to overview of all running courses with exams + header('Location: ' . URLHelper::getURL('dispatch.php/vips/exam_mode')); + sess()->save(); + die(); + } + } else { + PageLayout::addHeadElement( + 'script', + [], + 'STUDIP.JSUpdater.register("vips", () => location.reload());' + ); + } + } + } + + /** + * Return whether or not the current user has the given status in a course. + * + * @param string $status status name: 'autor', 'tutor' or 'dozent' + * @param string $course_id course to check + */ + public static function hasStatus(string $status, string $course_id): bool + { + return $course_id && $GLOBALS['perm']->have_studip_perm($status, $course_id); + } + + /** + * Check whether or not the current user has the required status in a course. + * + * @param string $status required status: 'autor', 'tutor' or 'dozent' + * @param string $course_id course to check + * @throws AccessDeniedException if the requirement is not met, an exception is thrown + */ + public static function requireStatus(string $status, string $course_id): void + { + if (!VipsModule::hasStatus($status, $course_id)) { + throw new AccessDeniedException(_('Sie verfügen nicht über die notwendigen Rechte für diese Aktion.')); + } + } + + /** + * Checks whether or not the current user may view an assignment. + * + * @param VipsAssignment|null $assignment assignment to check + * @param int|null $exercise_id check that this exercise is on the assignment (optional) + * @throws AccessDeniedException If the current user doesn't have access, an exception is thrown + */ + public static function requireViewPermission(?VipsAssignment $assignment, ?int $exercise_id = null): void + { + if (!$assignment || !$assignment->checkViewPermission()) { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf dieses Aufgabenblatt!')); + } + + if ($exercise_id && !$assignment->hasExercise($exercise_id)) { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf diese Aufgabe!')); + } + } + + /** + * Checks whether or not the current user may edit an assignment. + * + * @param VipsAssignment|null $assignment assignment to check + * @param int|null $exercise_id check that this exercise is on the assignment (optional) + * @throws AccessDeniedException If the current user doesn't have access, an exception is thrown + */ + public static function requireEditPermission(?VipsAssignment $assignment, ?int $exercise_id = null): void + { + if (!$assignment || !$assignment->checkEditPermission()) { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf dieses Aufgabenblatt!')); + } + + if ($exercise_id && !$assignment->hasExercise($exercise_id)) { + throw new AccessDeniedException(_('Sie haben keinen Zugriff auf diese Aufgabe!')); + } + } + + /** + * Get all courses where the user is at least tutor and Vips is activated. + * + * @return array with all course ids, null if no courses + */ + public static function getActiveCourses(string $user_id): array + { + $plugin_manager = PluginManager::getInstance(); + $vips_plugin_id = VipsModule::$instance->getPluginId(); + + $sql = "JOIN seminar_user USING(Seminar_id) + WHERE user_id = ? AND seminar_user.status IN ('dozent', 'tutor') + ORDER BY (SELECT MIN(beginn) FROM semester_data + JOIN semester_courses USING(semester_id) + WHERE course_id = Seminar_id) DESC, Name"; + $courses = Course::findBySQL($sql, [$user_id]); + + // remove courses where Vips is not active + foreach ($courses as $key => $course) { + if (!$plugin_manager->isPluginActivated($vips_plugin_id, $course->id)) { + unset($courses[$key]); + } + } + + return $courses; + } + + /** + * Get all courses with currently running exams for the given user. + * + * @param string $user_id The user id + * + * @return array associative array of course ids and course names + */ + public static function getCoursesWithRunningExams(string $user_id): array + { + $db = DBManager::get(); + + $courses = []; + + $sql = "SELECT DISTINCT seminare.Seminar_id, seminare.Name, etask_assignments.id + FROM etask_assignments + JOIN seminar_user ON seminar_user.Seminar_id = etask_assignments.range_id + JOIN seminare USING(Seminar_id) + WHERE etask_assignments.type = 'exam' + AND etask_assignments.start <= UNIX_TIMESTAMP() + AND etask_assignments.end > UNIX_TIMESTAMP() + AND seminar_user.user_id = ? + AND seminar_user.status = 'autor' + ORDER BY seminare.Name"; + $stmt = $db->prepare($sql); + $stmt->execute([$user_id]); + + foreach ($stmt as $row) { + $assignment = VipsAssignment::find($row['id']); + $ip_range = $assignment->options['ip_range']; + + if ($assignment->isVisible($user_id)) { + if (strlen($ip_range) > 0 && $assignment->checkIPAccess($_SERVER['REMOTE_ADDR'])) { + $courses[$row['Seminar_id']] = $row['Name']; + } + } + } + + return $courses; + } + + public function setupExamNavigation() + { + $navigation = new Navigation(''); + + $start = Navigation::getItem('/start'); + $start->setURL('dispatch.php/vips/exam_mode'); + $navigation->addSubNavigation('start', $start); + + $course = new Navigation(_('Veranstaltung')); + $navigation->addSubNavigation('course', $course); + + $vips = new Navigation($this->getPluginName()); + $vips->setImage(Icon::create('vips')); + $course->addSubNavigation('vips', $vips); + + $nav_item = new Navigation(_('Aufgabenblätter'), 'dispatch.php/vips/sheets'); + $vips->addSubNavigation('sheets', $nav_item); + + $links = new Navigation('Links'); + $links->addSubNavigation('logout', new Navigation(_('Logout'), 'logout.php')); + $navigation->addSubNavigation('links', $links); + + Config::get()->PERSONAL_NOTIFICATIONS_ACTIVATED = 0; + PageLayout::addStyle('#navigation-level-1, #navigation-level-2, #context-title { display: none; }'); + PageLayout::addCustomQuicksearch('
'); + Navigation::setRootNavigation($navigation); + } + + public function getIconNavigation($course_id, $last_visit, $user_id) + { + if (VipsModule::hasStatus('tutor', $course_id)) { + // find all uncorrected exercises in finished assignments in this course + // Added JOIN with seminar_user to filter out lecturer/tutor solutions. + $new_items = VipsSolution::countBySql( + "JOIN etask_assignments ON etask_responses.assignment_id = etask_assignments.id + LEFT JOIN seminar_user + ON seminar_user.Seminar_id = etask_assignments.range_id + AND seminar_user.user_id = etask_responses.user_id + WHERE etask_assignments.range_id = ? + AND etask_assignments.type IN ('exam', 'practice', 'selftest') + AND etask_assignments.end <= UNIX_TIMESTAMP() + AND etask_responses.state = 0 + AND IFNULL(seminar_user.status, 'autor') = 'autor'", + [$course_id] + ); + + $message = ngettext('%d unkorrigierte Lösung', '%d unkorrigierte Lösungen', $new_items); + } else { + // find all active assignments not yet seen by the student + $assignments = VipsAssignment::findBySQL( + "LEFT JOIN etask_assignment_attempts + ON etask_assignment_attempts.assignment_id = etask_assignments.id + AND etask_assignment_attempts.user_id = ? + WHERE etask_assignments.range_id = ? + AND etask_assignments.type IN ('exam', 'practice', 'selftest') + AND etask_assignments.start <= UNIX_TIMESTAMP() + AND (etask_assignments.end IS NULL OR etask_assignments.end > UNIX_TIMESTAMP()) + AND etask_assignment_attempts.user_id IS NULL", + [$user_id, $course_id] + ); + + $new_items = 0; + + foreach ($assignments as $assignment) { + if ($assignment->isVisible($user_id)) { + ++$new_items; + } + } + + $message = ngettext('%d neues Aufgabenblatt', '%d neue Aufgabenblätter', $new_items); + } + + $overview_message = $this->getPluginName(); + $icon = Icon::create('vips'); + + if ($new_items > 0) { + $overview_message = sprintf($message, $new_items); + $icon = Icon::create('vips', Icon::ROLE_NEW); + } + + $icon_navigation = new Navigation($this->getPluginName(), 'dispatch.php/vips/sheets'); + $icon_navigation->setImage($icon->copyWithAttributes(['title' => $overview_message])); + + return $icon_navigation; + } + + public function getInfoTemplate($course_id) + { + return null; + } + + public function getTabNavigation($course_id) + { + $navigation = new Navigation($this->getPluginName()); + $navigation->setImage(Icon::create('vips')); + + $nav_item = new Navigation(_('Aufgabenblätter'), 'dispatch.php/vips/sheets'); + $navigation->addSubNavigation('sheets', $nav_item); + + $nav_item = new Navigation(_('Ergebnisse'), 'dispatch.php/vips/solutions'); + $navigation->addSubNavigation('solutions', $nav_item); + + return ['vips' => $navigation]; + } + + public function getMetadata() + { + $metadata['category'] = _('Inhalte und Aufgabenstellungen'); + $metadata['displayname'] = _('Aufgaben und Prüfungen'); + $metadata['summary'] = + _('Erstellung und Durchführung von Übungen, Tests und Klausuren'); + $metadata['description'] = + _('Mit diesem Werkzeug können Übungen, Tests und Klausuren online vorbereitet und durchgeführt werden. ' . + 'Die Lehrenden erhalten eine Übersicht darüber, welche Teilnehmenden eine Übung oder einen ' . + 'Test mit welchem Ergebnis abgeschlossen haben. Im Gegensatz zu herkömmlichen Übungszetteln ' . + 'oder Klausurbögen sind in Stud.IP alle Texte gut lesbar und sortiert abgelegt. Lehrende ' . + 'erhalten sofort einen Überblick darüber, was noch zu korrigieren ist. Neben allgemein ' . + 'üblichen Fragetypen wie Multiple Choice und Freitextantwort verfügt das Werkzeug auch über ' . + 'ungewöhnlichere, aber didaktisch durchaus sinnvolle Fragetypen wie Lückentext und Zuordnung.'); + $metadata['keywords'] = + _('Einsatz bei Hausaufgaben und Präsenzprüfungen; Reduzierter Arbeitsaufwand bei der Auswertung; ' . + 'Sortierte Übersicht der eingereichten Ergebnisse; Single-, Multiple-Choice- und Textaufgaben, ' . + 'Lückentexte und Zuordnungen; Notwendige Korrekturen und erzielte Punktzahlen auf einen Blick'); + $metadata['icon'] = Icon::create('vips'); + + return $metadata; + } + + public function userDidDelete($event, $user) + { + // delete all personal assignments + VipsAssignment::deleteBySQL('range_id = ?', [$user->id]); + + // delete in etask_responses + VipsSolution::deleteBySQL('user_id = ?', [$user->id]); + + // delete start times and group memberships + VipsAssignmentAttempt::deleteBySQL('user_id = ?', [$user->id]); + VipsGroupMember::deleteBySQL('user_id = ?', [$user->id]); + } + + public function courseDidDelete($event, $course) + { + // delete all assignments in course + VipsAssignment::deleteBySQL('range_id = ?', [$course->id]); + + // delete other course related info + VipsBlock::deleteBySQL('range_id = ?', [$course->id]); + } + + public function userDidLeaveCourse($event, $course_id, $user_id) + { + // terminate group membership when leaving a course + $group_member = VipsGroupMember::findOneBySQL( + 'JOIN statusgruppen ON statusgruppe_id = group_id WHERE range_id = ? AND user_id = ? AND end IS NULL', + [$course_id, $user_id] + ); + + if ($group_member) { + $group_member->end = time(); + $group_member->store(); + } + } + + public function userDidMigrate($event, $user_id, $new_id) + { + $db = DBManager::get(); + + $db->execute('UPDATE IGNORE etask_assignment_attempts SET user_id = ? WHERE user_id = ?', [$new_id, $user_id]); + $db->execute('UPDATE etask_tasks SET user_id = ? WHERE user_id = ?', [$new_id, $user_id]); + + $db->execute('UPDATE IGNORE etask_responses SET user_id = ? WHERE user_id = ?', [$new_id, $user_id]); + $db->execute('UPDATE etask_tests SET user_id = ? WHERE user_id = ?', [$new_id, $user_id]); + } + + public function statusgruppeUserDidCreate($event, $statusgruppe_user) + { + VipsGroupMember::create([ + 'group_id' => $statusgruppe_user->statusgruppe_id, + 'user_id' => $statusgruppe_user->user_id, + 'start' => time() + ]); + } + + public function statusgruppeUserDidDelete($event, $statusgruppe_user) + { + $member = VipsGroupMember::findOneBySQL( + 'group_id = ? AND user_id = ? AND end IS NULL', + [$statusgruppe_user->statusgruppe_id, $statusgruppe_user->user_id] + ); + + if ($member) { + $member->end = time(); + $member->store(); + } + } + + /** + * Export available data of a given user into a storage object + * (an instance of the StoredUserData class) for that user. + * + * @param StoredUserData $store object to store data into + */ + public function exportUserData(StoredUserData $store) + { + $db = DBManager::get(); + + $data = $db->fetchAll('SELECT * FROM etask_group_members WHERE user_id = ?', [$store->user_id]); + $store->addTabularData(_('Aufgaben-Gruppenzuordnung'), 'etask_group_members', $data); + } + + /** + * Implement this method to register more block types. + * + * You get the current list of block types and return an updated list + * containing your own block types. + */ + public function registerBlockTypes(array $otherBlockTypes): array + { + $otherBlockTypes[] = Courseware\BlockTypes\TestBlock::class; + + return $otherBlockTypes; + } + + /** + * Implement this method to register more container types. + * + * You get the current list of container types and return an updated list + * containing your own container types. + */ + public function registerContainerTypes(array $otherContainerTypes): array + { + return $otherContainerTypes; + } +} diff --git a/public/assets/images/choice_checked.svg b/public/assets/images/choice_checked.svg new file mode 100644 index 0000000..ba483b5 --- /dev/null +++ b/public/assets/images/choice_checked.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/choice_unchecked.svg b/public/assets/images/choice_unchecked.svg new file mode 100644 index 0000000..4fa3b2a --- /dev/null +++ b/public/assets/images/choice_unchecked.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/collapse.svg b/public/assets/images/collapse.svg new file mode 100644 index 0000000..9aba4d5 --- /dev/null +++ b/public/assets/images/collapse.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/expand.svg b/public/assets/images/expand.svg new file mode 100644 index 0000000..12c5fa1 --- /dev/null +++ b/public/assets/images/expand.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/icons/black/vips.svg b/public/assets/images/icons/black/vips.svg new file mode 100644 index 0000000..fdd66a7 --- /dev/null +++ b/public/assets/images/icons/black/vips.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/icons/blue/assessment-mc.svg b/public/assets/images/icons/blue/assessment-mc.svg new file mode 100644 index 0000000..cb919d8 --- /dev/null +++ b/public/assets/images/icons/blue/assessment-mc.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/icons/blue/edit-line.svg b/public/assets/images/icons/blue/edit-line.svg new file mode 100644 index 0000000..b8e2f4a --- /dev/null +++ b/public/assets/images/icons/blue/edit-line.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/icons/blue/vips.svg b/public/assets/images/icons/blue/vips.svg new file mode 100644 index 0000000..35dae00 --- /dev/null +++ b/public/assets/images/icons/blue/vips.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/icons/red/vips.svg b/public/assets/images/icons/red/vips.svg new file mode 100644 index 0000000..49c4cae --- /dev/null +++ b/public/assets/images/icons/red/vips.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/icons/white/vips.svg b/public/assets/images/icons/white/vips.svg new file mode 100644 index 0000000..516901d --- /dev/null +++ b/public/assets/images/icons/white/vips.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/assets/images/plus/screenshots/Vips/Vips_preview_1.png b/public/assets/images/plus/screenshots/Vips/Vips_preview_1.png new file mode 100644 index 0000000..0673245 Binary files /dev/null and b/public/assets/images/plus/screenshots/Vips/Vips_preview_1.png differ diff --git a/public/assets/images/plus/screenshots/Vips/Vips_preview_2.png b/public/assets/images/plus/screenshots/Vips/Vips_preview_2.png new file mode 100644 index 0000000..54322a1 Binary files /dev/null and b/public/assets/images/plus/screenshots/Vips/Vips_preview_2.png differ diff --git a/public/assets/images/plus/screenshots/Vips/Vips_preview_3.png b/public/assets/images/plus/screenshots/Vips/Vips_preview_3.png new file mode 100644 index 0000000..7b1c2b2 Binary files /dev/null and b/public/assets/images/plus/screenshots/Vips/Vips_preview_3.png differ diff --git a/resources/assets/javascripts/bootstrap/vips.js b/resources/assets/javascripts/bootstrap/vips.js new file mode 100644 index 0000000..69eb34c --- /dev/null +++ b/resources/assets/javascripts/bootstrap/vips.js @@ -0,0 +1,336 @@ +import { $gettext } from "../lib/gettext"; + +$(function() { + if ($('#exam_timer').length > 0) { + const exam_timer = $('#exam_timer'); + const user_end_time = exam_timer.data('time') + Math.floor(Date.now() / 1000); + const timer_id = setInterval(() => { + const remaining_time = user_end_time - Math.floor(Date.now() / 1000); + + // update timer + exam_timer.children('.time').text(Math.round(remaining_time / 60)); + + if (remaining_time < 180 && !exam_timer.hasClass('alert')) { + exam_timer.addClass('alert'); + } + + if (remaining_time < 0) { + if (document.jsfrm) { + clearInterval(timer_id); + document.jsfrm.removeAttribute('data-secure'); + document.jsfrm.forced.value = 1; + document.jsfrm.submit(); + } else { + location.reload(); + } + } + }, 1000); + + exam_timer.draggable(); + } + + if ($('#list').length > 0) { + const assignment = $('#list').data('assignment'); + + $('#list').sortable({ + axis: 'y', + containment: 'parent', + handle: '.drag-handle', + helper(event, element) { + element.children().width((index, width) => width); + + return element; + }, + tolerance: 'pointer', + update() { + $.post( + STUDIP.URLHelper.getURL('dispatch.php/vips/sheets/move_exercise', { assignment_id: assignment }), + $('#list').sortable('serialize') + ); + } + }); + + $('#list > tr').on('keydown', function (event) { + if (event.key === 'ArrowUp' && event.target === this) { + $(this).prev().before(this); + } else if (event.key === 'ArrowDown' && event.target === this) { + $(this).next().after(this); + } else { + return; + } + + $(this).focus(); + $('#list').sortable('option').update(); + event.preventDefault(); + }); + } + + $(document).on('click', '.add_ip_range', function (event) { + const input = $(this).closest('fieldset').find('input[name=ip_range]'); + + input.val(input.val() + ' ' + $(this).attr('data-value')); + event.preventDefault(); + }); + + $(document).on('input', '.validate_ip_range', function () { + const ip_ranges = $(this).val().split(/[ ,]+/); + let message = ''; + + for (const ip_range of ip_ranges) { + if ( + ip_range.length > 0 + && ip_range.charAt(0) !== '#' + && !ip_range.match(/^[\d.]+(\/\d+|-[\d.]+)?$/) + && !ip_range.match(/^[\da-fA-F:]+(\/\d+|-[\da-fA-F:]+)?$/) + ) { + message = $gettext('Der IP-Zugriffsbereich ist ungültig.'); + } + } + + this.setCustomValidity(message); + }); + + $(document).on('click', '.vips_file_upload', function (event) { + $(this).closest('form').find('.file_upload').click(); + event.preventDefault(); + }); + + $(document).on('change', '.file_upload.attach', function () { + const button = $(this).closest('form').find('.vips_file_upload'); + + if (this.files && this.files.length > 1) { + button.text(button.data('label').replace('%d', this.files.length)); + button.next('.file_upload_hint').show(); + } else if (this.files) { + button.text(this.files[0].name); + button.next('.file_upload_hint').show(); + } + }); + + $(document).on('change', '.file_upload.inline', function (event) { + const textarea = $(this).closest('form').find('.download'); + const reader = new FileReader(); + + if (this.files && this.files.length > 0) { + reader.onload = function () { + textarea.val(reader.result); + }; + reader.onerror = function () { + STUDIP.Dialog.show(reader.error.message, { + title: $gettext('Fehler beim Hochladen'), + size: 'fit', + wikilink: false, + dialogClass: 'studip-confirmation' + }); + } + reader.readAsText(this.files[0]); + } + event.preventDefault(); + }); + + $(document).on('click', '.vips_file_download', function (event) { + const text = $(this).closest('form').find('.download').val(); + const link = $(this).closest('form').find('a[download]'); + const blob = new Blob([text], {type: 'text/plain; charset=UTF-8'}); + + link.attr('href', URL.createObjectURL(blob)); + link[0].click(); + event.preventDefault(); + }); + + $('.sortable_list').sortable({ + axis: 'y', + containment: 'parent', + items: '> .sortable_item', + tolerance: 'pointer' + }); + + $(document).on('keydown', '.sortable_item', function (event) { + if (event.key === 'ArrowUp' && event.target === this) { + $(this).prev('.sortable_item:visible').before(this); + } else if (event.key === 'ArrowDown' && event.target === this) { + $(this).next('.sortable_item:visible').after(this); + } else { + return; + } + + $(this).focus(); + event.preventDefault(); + }); + + $(document).on('click', '.textarea_toggle', function (event) { + const toggle = $(this).closest('.size_toggle'); + const items = toggle.find('.character_input'); + + const name = items[0].name; + items[0].name = items[1].name; + items[1].name = name; + + const value = items[0].value; + items[0].value = items[1].value; + items[1].value = value; + + if (STUDIP.wysiwyg.getEditor && STUDIP.wysiwyg.getEditor(items[1])) { + STUDIP.wysiwyg.getEditor(items[1]).setData(value); + } + + toggle.toggleClass('size_large').toggleClass('size_small'); + event.preventDefault(); + }); + + $(document).on('change', '.tb_layout', function () { + const toggle = $(this).closest('fieldset').find('.size_toggle'); + + toggle.find('.small_input').toggleClass('monospace', $(this).val() === 'code'); + + if ( + $(this).val() === '' && toggle.hasClass('size_large') + || $(this).val() === 'code' && toggle.hasClass('size_large') + || $(this).val() === 'markup' && toggle.hasClass('size_small') + ) { + toggle.find('.textarea_toggle').click(); + } + }); + + $(document).on('click', '.choice_list .add_dynamic_row', function () { + $(this).closest('fieldset').find('.choice_select').each(function () { + const template = $(this).children('.template').last(); + const clone = template.clone(true).removeClass('template'); + const index = template.data('index'); + + template.data('index', index + 1); + clone.insertBefore(template); + clone.find('input[data-value]').each(function () { + $(this).attr('value', index); + $(this).removeAttr('data-value'); + }); + }); + }); + + $(document).on('change', '.choice_list input', function () { + const index = $(this).closest('.dynamic_row').data('index'); + const items = $(this).closest('fieldset').find('.choice_select'); + + items.children().filter(function () { + return $(this).data('index') === index; + }).children('span').text($(this).val()); + }); + + $(document).on('click', '.choice_list .delete_dynamic_row', function () { + const index = $(this).closest('.dynamic_row').data('index'); + const items = $(this).closest('fieldset').find('.choice_select'); + + items.children().filter(function () { + return $(this).data('index') === index; + }).remove(); + }); + + $('.dynamic_list').each(function () { + $(this).children('.dynamic_row').each(function (i) { + $(this).data('index', i); + }); + }); + + $(document).on('click', '.add_dynamic_row', function (event) { + const container = $(this).closest('.dynamic_list'); + const template = container.children('.template').last(); + const clone = template.clone(true).removeClass('template'); + const index = template.data('index'); + + template.data('index', index + 1); + clone.insertBefore(template); + clone.find('input[data-name], select[data-name], textarea[data-name]').each(function () { + if ($(this).data('name').indexOf(':') === 0) { + $(this).data('name', $(this).data('name').substr(1) + '[' + index + ']'); + } else { + $(this).attr('name', $(this).data('name') + '[' + index + ']'); + $(this).removeAttr('data-name'); + } + }); + clone.find('input[data-value], select[data-value], textarea[data-value]').each(function () { + if ($(this).data('value').indexOf(':') === 0) { + $(this).data('value', $(this).data('value').substr(1)); + } else { + $(this).attr('value', index); + $(this).removeAttr('data-value'); + } + }); + clone.find('.wysiwyg-hidden:not(.template *)').toggleClass('wysiwyg wysiwyg-hidden'); + clone.find('.add_dynamic_row:visible').click(); + event.preventDefault(); + }); + + $(document).on('click', '.delete_dynamic_row', function (event) { + $(this).closest('.dynamic_row').remove(); + event.preventDefault(); + }); + + $(document).on('click', '.solution-toggle', function (event) { + if ($(this).closest('.solution').length > 0) { + $(this).closest('.solution').toggleClass('solution-closed'); + } else if ($('.arrow_all').first().css('display') !== 'none') { + $('.arrow_all').toggle(); + $('.solution').removeClass('solution-closed'); + } else { + $('.arrow_all').toggle(); + $('.solution').addClass('solution-closed'); + } + + $(document.body).trigger('sticky_kit:recalc'); + event.preventDefault(); + }); + + $(document).on('click', '.edit_solution', function (event) { + const tabs = $(this).closest('.vips_tabs'); + + tabs.removeClass('edit-hidden'); + tabs.find('.wysiwyg').attr('name', 'commented_solution'); + tabs.tabs('option', 'active', 0); + event.preventDefault(); + }); + + // add select2 to modal dialog including selects with optgroups + $(document).on('dialog-open', function (event, parameters) { + $('.vips_nested_select').select2({ + minimumResultsForSearch: 12, + dropdownParent: $(parameters.dialog).closest('.ui-dialog, body'), + matcher(params, data) { + const originalMatcher = $.fn.select2.defaults.defaults.matcher; + const result = originalMatcher(params, data); + + if (result && result.children && data.children && data.children.length) { + if (data.children.length !== result.children.length && + data.text.toLowerCase().includes(params.term.toLowerCase())) { + result.children = data.children; + } + } + + return result; + } + }); + }); + + $('.assignment_type').change(function () { + $('#assignment').attr('class', $(this).val()); + + if ($(this).val() === 'exam') { + $('#exam_length input').attr('disabled', null); + } else { + $('#exam_length input').attr('disabled', 'disabled'); + } + + if ($(this).val() === 'selftest') { + $('#end_date input').attr('required', null); + $('#end_date span').removeClass('required'); + } else { + $('#end_date input').attr('required', 'required'); + $('#end_date span').addClass('required'); + } + }); + + $('.rh_select_type').change(function () { + $(this).parent().next('table').toggleClass('rh_single'); + }); + + STUDIP.Vips.vips_post_render(document); +}); diff --git a/resources/assets/javascripts/entry-base.js b/resources/assets/javascripts/entry-base.js index 102a558..2210471 100644 --- a/resources/assets/javascripts/entry-base.js +++ b/resources/assets/javascripts/entry-base.js @@ -77,6 +77,7 @@ import "./bootstrap/admin-courses.js" import "./bootstrap/oer.js" import "./bootstrap/courseware.js" import "./bootstrap/external_pages.js" +import "./bootstrap/vips.js" import "./mvv_course_wizard.js" import "./mvv.js" diff --git a/resources/assets/javascripts/init.js b/resources/assets/javascripts/init.js index 2103ca2..4af6ed9 100644 --- a/resources/assets/javascripts/init.js +++ b/resources/assets/javascripts/init.js @@ -78,6 +78,7 @@ import * as Gettext from './lib/gettext'; import UserFilter from './lib/user_filter.js'; import wysiwyg from './lib/wysiwyg.js'; import ScrollToTop from './lib/scroll_to_top.js'; +import * as Vips from './lib/vips.js'; const configURLHelper = _.get(window, 'STUDIP.URLHelper', {}); const URLHelper = createURLHelper(configURLHelper); @@ -165,5 +166,6 @@ window.STUDIP = _.assign(window.STUDIP || {}, { domReady, dialogReady, ScrollToTop, + Vips, Vue, }); diff --git a/resources/assets/javascripts/lib/vips.js b/resources/assets/javascripts/lib/vips.js new file mode 100644 index 0000000..aaff259 --- /dev/null +++ b/resources/assets/javascripts/lib/vips.js @@ -0,0 +1,122 @@ +function vips_post_render(element) { + $(element).find('.rh_list').sortable({ + tolerance: 'pointer', + connectWith: '.rh_list', + update(event, ui) { + if (ui.sender) { + ui.item.find('input').val($(this).data('group')); + } + }, + over() { + $(this).addClass('hover'); + }, + out() { + $(this).removeClass('hover'); + }, + receive(event, ui) { + const sortable = $(this).not('.multiple'); + const container = sortable.closest('.rh_table').find('.answer_container'); + + // default answer container can have more items + if (sortable.children().length > 1 && !sortable.is(container)) { + sortable.find('.rh_item').each(function () { + if (!ui.item.is(this)) { + $(this).find('input').val(-1); + $(this).detach().appendTo(container) + .css('opacity', 0).animate({opacity: 1}); + } + }); + } + }, + }); + + $(element).find('.rh_item').on('keydown', function (event) { + const sortable = $(this).parent(); + const container = sortable.closest('.rh_table').find('.answer_container'); + let target = $(); + + if (sortable.is('.mc_list')) { + if (event.key === 'ArrowUp') { + $(this).prev().before(this); + $(this).focus(); + event.preventDefault(); + } else if (event.key === 'ArrowDown') { + $(this).next().after(this); + $(this).focus(); + event.preventDefault(); + } + } else if (sortable.is(container)) { + if (event.key === 'ArrowLeft') { + target = sortable.parent().find('.rh_list').first(); + } + } else { + if (event.key === 'ArrowRight') { + target = container; + } else if (event.key === 'ArrowUp') { + target = sortable.parent().prev().find('.rh_list').first(); + } else if (event.key === 'ArrowDown') { + target = sortable.parent().next().find('.rh_list').first(); + } + } + + if (target.length) { + $(this).find('input').val(target.data('group')); + $(this).appendTo(target).focus(); + event.preventDefault(); + } + }); + + $(element).find('.cloze_select').filter(':contains("\\\\(")').each(function () { + STUDIP.loadChunk('mathjax').then(({ Hub }) => { + Hub.Queue(['Typeset', Hub, this]); + }); + }).select2({ + minimumResultsForSearch: -1, + templateResult(data) { + if ($(data.element).children('.MathJax').length) { + return $(data.element).children('.MathJax').clone(); + } else { + return data.text; + } + }, + templateSelection(data) { + if ($(data.element).children('.MathJax').length) { + return $(data.element).children('.MathJax').clone(); + } else { + return data.text; + } + } + }); + + $(element).find('.cloze_item').draggable({ + revert: 'invalid' + }); + + $(element).find('.cloze_drop').droppable({ + accept: '.cloze_item', + tolerance: 'pointer', + classes: { + 'ui-droppable-hover': 'hover' + }, + drop(event, ui) { + const container = $(this).closest('fieldset').find('.cloze_items'); + + if (!$(this).is(container)) { + $(this).find('.cloze_item').detach().appendTo(container) + .css('opacity', 0).animate({opacity: 1}) + } + + ui.draggable.closest('.cloze_drop').find('input').val(''); + ui.draggable.detach().css({top: 0, left: 0}).appendTo(this); + $(this).find('input').val(ui.draggable.attr('data-value')); + } + }); + + $(element).find('.vips_tabs').each(function () { + $(this).tabs({ + active: $(this).hasClass('edit-hidden') ? 1 : 0 + }); + }) +} + +export { vips_post_render }; diff --git a/resources/assets/stylesheets/scss/buttons.scss b/resources/assets/stylesheets/scss/buttons.scss index db400c2..55171f5 100644 --- a/resources/assets/stylesheets/scss/buttons.scss +++ b/resources/assets/stylesheets/scss/buttons.scss @@ -25,7 +25,7 @@ &:hover, &:active, &.active { - background: var(--color--button-focus); + background-color: var(--color--button-focus); color: var(--color--font-inverted); } diff --git a/resources/assets/stylesheets/scss/courseware/variables.scss b/resources/assets/stylesheets/scss/courseware/variables.scss index 033c99f..b3da454 100644 --- a/resources/assets/stylesheets/scss/courseware/variables.scss +++ b/resources/assets/stylesheets/scss/courseware/variables.scss @@ -74,6 +74,7 @@ $blockadder-items: ( key-point: exclaim-circle, link: link-extern, table-of-contents: table-of-contents, + test: check-circle, text: edit, timeline: date-cycle, typewriter: block-typewriter, diff --git a/resources/assets/stylesheets/scss/forms.scss b/resources/assets/stylesheets/scss/forms.scss index 5c8f3a4..1cb037f 100644 --- a/resources/assets/stylesheets/scss/forms.scss +++ b/resources/assets/stylesheets/scss/forms.scss @@ -136,6 +136,7 @@ form.default { } .formpart { + display: block; margin-bottom: $gap; output.calculator_result { @@ -198,7 +199,7 @@ form.default { margin-top: 2ex; } - fieldset { + fieldset:not(.undecorated) { box-sizing: border-box; border: solid 1px var(--color--fieldset-border); margin: 0 0 15px; @@ -229,6 +230,16 @@ form.default { } } + fieldset.undecorated { + border: none; + margin: 0; + padding: 0; + + > legend { + margin-bottom: 0.5ex; + } + } + .selectbox { padding: 5px; max-height: 200px; diff --git a/resources/assets/stylesheets/scss/jquery-ui/studip.scss b/resources/assets/stylesheets/scss/jquery-ui/studip.scss index db487f0..002eb17 100644 --- a/resources/assets/stylesheets/scss/jquery-ui/studip.scss +++ b/resources/assets/stylesheets/scss/jquery-ui/studip.scss @@ -209,3 +209,45 @@ textarea.ui-resizable-handle.ui-resizable-s { background-color: var(--base-color); } } + +.ui-tabs { + &.ui-widget-content { + border: 1px solid var(--light-gray-color-40); + margin-top: 1.5ex; + padding: 0; + } + + .ui-tabs-nav { + background: none; + border: none; + border-bottom: 1px solid var(--light-gray-color-40); + } + + .ui-tabs-tab { + background: none; + border: none; + } + + .ui-tabs-tab:hover { + border-bottom: 3px solid var(--dark-gray-color-40); + } + + .ui-tabs-nav li.ui-tabs-active { + margin-bottom: 0; + padding-bottom: 0; + border-bottom: 3px solid var(--light-gray-color-80); + } + + .ui-tabs-tab .ui-tabs-anchor { + color: var(--base-color); + padding: 5px 15px; + } + + .ui-tabs-active .ui-tabs-anchor { + color: black; + } + + .ui-tabs-panel { + padding: 5px; + } +} diff --git a/resources/assets/stylesheets/scss/sidebar.scss b/resources/assets/stylesheets/scss/sidebar.scss index 1fcd1ce..0fcc32a 100644 --- a/resources/assets/stylesheets/scss/sidebar.scss +++ b/resources/assets/stylesheets/scss/sidebar.scss @@ -179,7 +179,7 @@ div#sidebar-navigation { .widget-links { margin: 5px; > li img { - vertical-align: text-top; + vertical-align: top; } a { display: block; diff --git a/resources/assets/stylesheets/scss/tables.scss b/resources/assets/stylesheets/scss/tables.scss index 32a26c2..788392d 100644 --- a/resources/assets/stylesheets/scss/tables.scss +++ b/resources/assets/stylesheets/scss/tables.scss @@ -85,6 +85,10 @@ td.blanksmall { background-color: var(--color--global-background); } +table.fixed { + table-layout: fixed; +} + td.tree-indent { img, svg { vertical-align: bottom; @@ -476,6 +480,15 @@ table.default { white-space: nowrap; } + img { + vertical-align: text-bottom; + } + + input[type="text"], textarea { + padding-bottom: 2px; + padding-top: 2px; + } + padding: 10px 5px; text-align: left; @@ -715,7 +728,7 @@ table.default { } } - tfoot { + tfoot, th { // Fix button and select alignment select { vertical-align: middle; diff --git a/resources/assets/stylesheets/scss/vips.scss b/resources/assets/stylesheets/scss/vips.scss new file mode 100644 index 0000000..f11afda --- /dev/null +++ b/resources/assets/stylesheets/scss/vips.scss @@ -0,0 +1,592 @@ +form.default { + .inline_select { + height: 32px; + width: auto; + } + + .label-text { + display: block; + margin: 1.5ex 0 0.5ex 0; + text-indent: 0.25ex; + } + + .vips_nested_select { + transition: inherit; + } + + input.cloze_input { + margin: 2px; + padding-bottom: 2px; + padding-top: 2px; + } + + select.cloze_select { + height: auto; + margin: 2px; + width: auto; + } + + input.percent_input { + text-align: right; + width: 4em; + } + + label:not(.undecorated) .select2-container { + display: block; + } +} + +button.vips_file_upload { + @include background-icon(upload); + background-position: 0.5em center; + background-repeat: no-repeat; + background-size: var(--icon-size-inline); + padding-left: 30px; + + &:hover { + @include background-icon(upload, info_alt); + } +} + +progress.assignment { + appearance: none; + background: var(--light-gray-color-20); + border: none; + height: 8px; + width: 120px; + + &::-moz-progress-bar { + background: var(--base-color); + } + &::-webkit-progress-bar { + background: var(--light-gray-color-20); + } + &::-webkit-progress-value { + background: var(--base-color); + } +} + +.vips-teaser { + background-color: var(--content-color-20); + background-image: url(../images/icons/blue/vips.svg); + background-position: 64px 50%; + background-repeat: no-repeat; + background-size: 120px; + max-width: 562px; + padding: 24px 24px 24px 244px; + + header { + font-size: 1.5em; + margin-bottom: 0.5em; + } +} + +.width-1200 { + max-width: 1200px; +} + +.breadcrumb { + margin-bottom: 1ex; + + img { + vertical-align: text-bottom; + } +} + +.smaller { + font-size: smaller; +} + +.monospace, +.vips_tabs .monospace { + font-family: monospace; +} + +.vips_tabs .vips_output { + background: none; +} + +.vips_output { + background-color: var(--dark-gray-color-5); + max-height: 30em; + min-height: 1em; + overflow-y: auto; + padding: 3px; + + pre { + margin: 2px; + white-space: pre-wrap; + } +} + +.sidebar_exercise_label { + display: inline-block; + width: 120px; +} + +.sidebar_exercise_points { + display: inline-block; + text-align: right; + width: 80px; +} + +.sidebar_exercise_state { + display: inline-block; + text-align: right; + width: 32px; +} + +.sortable .gradebook_header { + font-size: smaller; + max-width: 8em; + overflow-x: hidden; + text-align: right; + text-overflow: ellipsis; + white-space: nowrap; +} + +.sortable_item { + padding-left: 2ex; +} + +.exercise_types { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + max-width: 50em; +} + +.exercise_type { + background-color: transparent; + background-position: 0.5em center; + background-repeat: no-repeat; + border: 1px solid var(--color--fieldset-border); + color: var(--base-color); + cursor: pointer; + margin-top: 1.5ex; + min-height: 50px; + padding: 4px 4px 4px 56px; + text-align: left; + width: 342px; + + &:hover { + border: 1px solid var(--brand-color-dark); + color: var(--active-color); + } +} + +.exercise .points { + float: right; + font-size: 14px; + font-weight: normal; +} + +#exercises .points { + text-align: right; + width: 4em; +} + +.exercise_hint { + background: var(--activity-color-20); + border: 1px dotted var(--dark-gray-color-75); + display: inline-block; + margin-top: 1.5ex; + padding: 1ex; + + > h4 { + margin-top: 0px; + } +} + +#exam_timer { + background-color: var(--white); + border: 3px solid var(--red); + cursor: move; + font-size: 1.1em; + left: calc(100% - 134px); + margin: 3px; + padding: 3px; + position: fixed; + top: 0px; + white-space: nowrap; + z-index: 10001; + + &.alert { + color: var(--red); + } +} + +.inline-block { + display: inline-block; +} + +.inline-content .formatted-content { + display: inline-block; + vertical-align: top; + + > p:last-child { + margin-bottom: 0; + } +} + +.print_settings { + background-color: var(--activity-color-20); + border-bottom: 1px dotted var(--dark-gray-color-75); + display: none; + margin: -2em -2em 1em -2em; + padding: 1ex 2em; + + label { + margin-left: 1em; + } +} + +.choice_select { + display: inline-block; + padding: 0 1ex; +} + +.rh_single .rh_add_answer:not(:nth-child(2)) { + display: none; +} + +.rh_table { + border-spacing: 3em 1em; + margin-left: -2em; +} + +.rh_list { + background-color: var(--dark-gray-color-5); + border: 1px dashed var(--dark-gray-color-45); + min-width: 160px; + + &.hover { + border-color: var(--black); + } +} + +.rh_label { + padding-bottom: 2ex; + padding-top: 2ex; +} + +.rh_item { + background-color: var(--white); + border: 1px solid var(--base-color-20); + margin: 4px; + padding: 1ex 1ex 1ex 2ex; + + &:hover { + border-color: var(--dark-gray-color-45); + } +} + +.cloze_drop { + background-color: var(--dark-gray-color-5); + border: 1px dashed var(--dark-gray-color-45); + display: inline-block; + min-height: 32px; + min-width: 80px; + vertical-align: middle; + white-space: normal; + + &.hover { + border-color: var(--black); + } + + &.cloze_items { + display: block; + margin-top: 1em; + padding: 2px; + } +} + +.cloze_item { + background-color: var(--white); + background-size: auto 22px; + border: 1px solid var(--base-color-20); + display: inline-block; + margin: 2px; + min-width: 48px; + padding: 3px 1ex 3px 2ex; + + &:hover { + border-color: var(--dark-gray-color-45); + } +} + +.mc_row { + margin: 1ex 0; + + img, + input[type="image"] { + vertical-align: text-bottom; + } +} + +.mc_list { + display: inline-block; + margin-top: 1.5ex; + min-width: 32em; +} + +.mc_item { + padding: 4px; +} + +.mc_flex, +form.default label.mc_flex { + align-items: start; + column-gap: 6px; + display: flex; +} + +.mc_flex > img:first-child { + padding: 2px; +} + +.mc_flex > .formatted-content { + flex-grow: 1; +} + +.correct_item { + background: var(--green-20); + border: 1px solid var(--green-40); + padding: 3px; +} + +.fuzzy_item { + background: var(--yellow-20); + border: 1px solid var(--yellow-40); + padding: 3px; +} + +.wrong_item { + background: var(--red-20); + border: 1px solid var(--red-40); + padding: 3px; +} + +.neutral_item { + border: 1px dotted var(--dark-gray-color-30); + margin-right: 2.5em; + padding: 3px; +} + +.correction_marker { + float: right; + margin-left: 1em; + + &.sequence { + font-size: 20px; + margin-top: -15px; + } +} + +.correction_inline { + vertical-align: text-bottom; +} + +.group_separator { + border-top: 1px dotted var(--dark-gray-color-75); + margin-top: 1.5ex; + padding-top: 1.5ex; +} + +.dynamic_list { + counter-reset: vips_item; +} + +.dynamic_counter::before { + counter-increment: vips_item; + content: counter(vips_item) "."; +} + +.dynamic_row:first-child .hide_first { + display: none; +} + +.template { + display: none; +} + +.solution-close { + display: inline; +} + +.solution-open { + display: none; +} + +.solution-closed { + + tr { + display: none; + } + + .solution-close { + display: none; + } + + .solution-open { + display: inline; + } +} + +.solution { + vertical-align: top; +} + +.solution-col-5 { + vertical-align: top; + width: 20%; +} + +.solution-none { + color: var(--light-gray-color); +} + +.solution-uncorrected { + color: var(--red); +} + +.solution-autocorrected { + color: var(--petrol); +} + +.solution-corrected { + color: var(--green); + font-weight: bold; +} + +.vips_tabs.edit-hidden .edit-tab { + display: none; +} + +#assignment.exam .exam-hidden, +#assignment.practice .practice-hidden, +#assignment.selftest .selftest-hidden { + display: none; +} + +#list > tr:first-child .icon-shape-arr_2up, +#list > tr:last-child .icon-shape-arr_2down { + visibility: hidden; +} + +.options-toggle { + display: none; +} + +.options-toggle + .caption + .toggle-box, +.options-toggle + .caption > .toggle-open, +.options-toggle:checked + .caption > .toggle-closed { + display: none; +} + +.options-toggle:checked + .caption + .toggle-box, +.options-toggle:checked + .caption > .toggle-open { + display: initial; +} + +.options-toggle + .caption { + background-color: var(--color--fieldset-header); + color: var(--brand-color-dark); + display: block; + font-weight: bold; + margin-bottom: 1.5ex; + padding: 4px; + + > img { + vertical-align: text-bottom; + } +} + +table.default input.small_input { + margin-bottom: 2px; +} + +.size_toggle { + &.size_small .large_input, + &.size_large .small_input { + display: none; + } + + .flexible_input { + display: inline-block; + max-width: 48em; + vertical-align: top; + width: 91%; + } +} + +.vs__selected img, +.vs__dropdown-option img { + vertical-align: text-bottom; +} + +.vs__dropdown-option small { + margin-left: 20px; +} + +.cw-exercise-header { + display: flex; + height: 20px; + + span { + flex-grow: 1; + } +} + +.cw-exercise-fieldset header { + background-color: var(--color--fieldset-header); + color: var(--brand-color-dark); + font-weight: 600; + margin: 14px 0 8px -10px; + padding: 6px 10px; +} + +#vips-sheets-print_assignments { + display: block; + min-height: auto; + width: auto; + + @media screen { + margin: 2em; + + .print_settings { + display: block; + } + } + + footer { + display: none; + } + + table.content th { + padding: 3px; + text-align: left; + } + + ol, ul { + padding-left: 30px; + } + + .assignment { + page-break-after: always; + } + + .exercise { + margin-bottom: 1em; + page-break-inside: avoid; + } + + .label-text { + font-weight: bold; + margin-top: 1.5ex; + } + + .vips_output { + background: none; + max-height: none; + } +} diff --git a/resources/assets/stylesheets/studip.scss b/resources/assets/stylesheets/studip.scss index 624e99a..9183081 100644 --- a/resources/assets/stylesheets/studip.scss +++ b/resources/assets/stylesheets/studip.scss @@ -105,6 +105,7 @@ @import "scss/tree"; @import "scss/typography"; @import "scss/user-administration"; +@import "scss/vips"; @import "scss/wiki"; @import "scss/multi_person_search"; @import "scss/admission"; diff --git a/resources/vue/components/courseware/blocks/CoursewareTestBlock.vue b/resources/vue/components/courseware/blocks/CoursewareTestBlock.vue new file mode 100644 index 0000000..9c90291 --- /dev/null +++ b/resources/vue/components/courseware/blocks/CoursewareTestBlock.vue @@ -0,0 +1,266 @@ + + + diff --git a/resources/vue/components/courseware/containers/container-components.js b/resources/vue/components/courseware/containers/container-components.js index 0a89bef..c920dae 100644 --- a/resources/vue/components/courseware/containers/container-components.js +++ b/resources/vue/components/courseware/containers/container-components.js @@ -27,6 +27,7 @@ import CoursewareKeyPointBlock from '../blocks/CoursewareKeyPointBlock.vue'; import CoursewareLinkBlock from '../blocks/CoursewareLinkBlock.vue'; import CoursewareLtiBlock from '../blocks/CoursewareLtiBlock.vue'; import CoursewareTableOfContentsBlock from '../blocks/CoursewareTableOfContentsBlock.vue'; +import CoursewareTestBlock from '../blocks/CoursewareTestBlock.vue'; import CoursewareTextBlock from '../blocks/CoursewareTextBlock.vue'; import CoursewareTimelineBlock from '../blocks/CoursewareTimelineBlock.vue'; import CoursewareTypewriterBlock from '../blocks/CoursewareTypewriterBlock.vue'; @@ -66,6 +67,7 @@ const ContainerComponents = { CoursewareLinkBlock, CoursewareLtiBlock, CoursewareTableOfContentsBlock, + CoursewareTestBlock, CoursewareTextBlock, CoursewareTimelineBlock, CoursewareTypewriterBlock, -- cgit v1.0