• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ProjektAdLer / MoodlePluginModAdleradaptivity / 11522992594

25 Oct 2024 05:48PM UTC coverage: 76.509% (-0.08%) from 76.591%
11522992594

push

github

Glutamat42
fix invalid parameter for required_param

1 of 1 new or added line in 1 file covered. (100.0%)

28 existing lines in 2 files now uncovered.

710 of 928 relevant lines covered (76.51%)

7.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/classes/output/view_renderer.php
1
<?php
2

3
namespace mod_adleradaptivity\output;
4

5
defined('MOODLE_INTERNAL') || die();
×
6

7
use coding_exception;
8
use completion_info;
9
use html_writer;
10
use mod_adleradaptivity\external\answer_questions;
11
use mod_adleradaptivity\local\completion_helpers;
12
use mod_adleradaptivity\local\helpers;
13
use moodle_exception;
14
use moodle_url;
15
use plugin_renderer_base;
16
use qbank_previewquestion\question_preview_options;
17
use question_usage_by_activity;
18
use stdClass;
19

20
class view_renderer extends plugin_renderer_base {
21
    /**
22
     * Renders the form with the tasks with its questions.
23
     *
24
     * @param array $tasks
25
     * @param question_usage_by_activity $quba
26
     * @param stdClass $cm
27
     * @param stdClass $course
28
     * @return string
29
     * @throws coding_exception
30
     * @throws moodle_exception
31
     */
32
    public function render_module_view_page(array $tasks, question_usage_by_activity $quba, stdClass $cm, stdClass $course): string {
33
        $cmid = $cm->id;
×
34

UNCOV
35
        $slots = $quba->get_slots();
×
36
        // Define the URL for form submission
UNCOV
37
        $actionurl = new moodle_url('/mod/adleradaptivity/processattempt.php', ['id' => $cmid, 'attempt' => $quba->get_id()]);
×
38

39
        // start generating output
40
        $output = '';
×
41

42

43
        // Start the question form with the action URL
44
        $output .= html_writer::start_tag('form', ['method' => 'post', 'action' => $actionurl->out(false), 'enctype' => 'multipart/form-data', 'id' => 'responseform']);
×
45

46
        // Hidden fields for sesskey and possibly other data need to pass through the form to the processattempt page
47
        $output .= html_writer::start_tag('div');
×
UNCOV
48
        $output .= html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
×
49
        $output .= html_writer::end_tag('div');
×
50

UNCOV
51
        $output .= $this->render_content($tasks, $quba, $cm, $course);
×
52

UNCOV
53
        $output .= html_writer::end_tag('form');
×
54

55

UNCOV
56
        return $output;
×
57
    }
58

59
    /**
60
     * Renders the tasks with its questions of the adleradaptivity module.
61
     *
62
     * @param array $tasks Array of task objects with associated questions.
63
     * @return string HTML to output.
64
     * @throws coding_exception If getting the translation string failed
65
     * @throws moodle_exception If the question cannot be rendered.
66
     */
67
    private function render_content($tasks, question_usage_by_activity $quba, stdClass $cm, stdClass $course): string {
68
        $completion = new completion_info($course);
×
69

UNCOV
70
        $data = [
×
71
            'module_completed' => helpers::determine_module_completion_status($completion, $cm) == completion_helpers::STATUS_CORRECT,
×
72
            'tasks' => []
×
UNCOV
73
        ];
×
74

75
        foreach ($tasks as $task_id => $task) {
×
76
            $task_status = completion_helpers::check_task_status($quba, $task_id, $task['required_difficulty']);
×
77

78
            $taskData = [
×
79
                'title' => $task['title'],
×
80
                'optional' => $task['required_difficulty'] === null,
×
81
                'difficulty' => $task['required_difficulty'] !== null ? $this->get_difficulty_label($task['required_difficulty']) : '',
×
82
                'status_success' => in_array($task_status, [completion_helpers::STATUS_CORRECT, completion_helpers::STATUS_OPTIONAL_INCORRECT, completion_helpers::STATUS_OPTIONAL_NOT_ATTEMPTED]),
×
UNCOV
83
                'status_message' => get_string($this->get_task_status_message_translation_key($task_status), 'mod_adleradaptivity'),
×
84
                'status_class' => $this->get_task_status_class($task_status),
×
85
                'questions' => []
×
86
            ];
×
87

UNCOV
88
            foreach ($task['questions'] as $question) {
×
89
                $options = new question_preview_options($question['question']);
×
90
                $options->load_user_defaults();
×
91
                $options->set_from_request();
×
92

UNCOV
93
                $taskData['questions'][] = [
×
UNCOV
94
                    'content' => $quba->render_question($question['slot'], $options, $this->get_difficulty_label($question['difficulty'])),
×
95
                    'status_best_try' => completion_helpers::check_question_answered_correctly_once($quba->get_question_attempt($question['slot'])),
×
UNCOV
96
                ];
×
97
            }
98

UNCOV
99
            $data['tasks'][] = $taskData;
×
100
        }
101

UNCOV
102
        return $this->render_from_template('mod_adleradaptivity/questions', $data);
×
103
    }
104

105
    /**
106
     * @param string $status One of the STATUS_* constants from completion_helpers.
107
     * @return string The translation key for the status message.
108
     */
109
    private function get_task_status_message_translation_key(string $status): string {
110
        return match ($status) {
×
111
            completion_helpers::STATUS_NOT_ATTEMPTED => 'view_task_status_not_attempted',
×
112
            completion_helpers::STATUS_CORRECT => 'view_task_status_correct',
×
113
            completion_helpers::STATUS_INCORRECT => 'view_task_status_incorrect',
×
UNCOV
114
            completion_helpers::STATUS_OPTIONAL_NOT_ATTEMPTED => 'view_task_status_optional_not_attempted',
×
UNCOV
115
            completion_helpers::STATUS_OPTIONAL_INCORRECT => 'view_task_status_optional_incorrect',
×
UNCOV
116
            default => 'view_task_status_unknown',
×
117
        };
×
118
    }
119

120
    private function get_task_status_class(string $status): string {
121
        return match ($status) {
×
122
            completion_helpers::STATUS_NOT_ATTEMPTED => 'task-not-attempted',
×
123
            completion_helpers::STATUS_CORRECT => 'task-correct',
×
124
            completion_helpers::STATUS_INCORRECT => 'task-incorrect',
×
UNCOV
125
            completion_helpers::STATUS_OPTIONAL_NOT_ATTEMPTED => 'task-optional-not-attempted',
×
UNCOV
126
            completion_helpers::STATUS_OPTIONAL_INCORRECT => 'task-optional-incorrect',
×
UNCOV
127
            default => 'unknown',
×
UNCOV
128
        };
×
129
    }
130

131
    /**
132
     * Converts a difficulty code to a human-readable label.
133
     *
134
     * @param int|null $difficulty The difficulty code.
135
     * @return string The difficulty label.
136
     * @throws coding_exception
137
     */
138
    private function get_difficulty_label(int|null $difficulty): string {
139
        $difficulties = [
×
UNCOV
140
            0 => get_string('difficulty_0', 'mod_adleradaptivity'),
×
141
            100 => get_string('difficulty_100', 'mod_adleradaptivity'),
×
UNCOV
142
            200 => get_string('difficulty_200', 'mod_adleradaptivity')
×
UNCOV
143
        ];
×
144

UNCOV
145
        return $difficulties[$difficulty] ?? 'unknown';
×
146
    }
147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc