Skip to content

Commit

Permalink
MDL-53009 Quiz: Replace maxmark on Edit quiz page
Browse files Browse the repository at this point in the history
with the inplace_editible element
  • Loading branch information
mkassaei committed May 10, 2024
1 parent 0fae175 commit 7174977
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 579 deletions.
23 changes: 7 additions & 16 deletions mod/quiz/classes/output/edit_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ public function question(structure $structure, int $slot, \moodle_url $pageurl)
'questionnumber' => $this->question_number($questionnumber, $structure->get_slot_by_number($slot)->defaultnumber),
'questionname' => $this->get_question_name_for_slot($structure, $slot, $pageurl),
'questionicons' => $this->get_action_icon($structure, $slot, $pageurl),
'questionmaxmark' => $structure->make_slot_maxmark_in_place_editable($structure->get_slot_id_for_slot($slot),
$structure->get_context()),
'questiondependencyicon' => ($structure->can_be_edited() ? $this->question_dependency_icon($structure, $slot) : ''),
'versionselection' => false,
'draftversion' => $structure->get_question_in_slot($slot)->status == question_version_status::QUESTION_STATUS_DRAFT,
Expand Down Expand Up @@ -1117,22 +1119,11 @@ public function marked_out_of_field(structure $structure, $slot) {
return html_writer::span($output, 'instancemaxmarkcontainer infoitem');
}

$output = html_writer::span($structure->formatted_question_grade($slot),
'instancemaxmark decimalplaces_' . $structure->get_decimal_places_for_question_marks(),
['title' => get_string('maxmark', 'quiz')]);

$output .= html_writer::span(
html_writer::link(
new \moodle_url('#'),
$this->pix_icon('t/editstring', '', 'moodle', ['class' => 'editicon visibleifjs', 'title' => '']),
[
'class' => 'editing_maxmark',
'data-action' => 'editmaxmark',
'title' => get_string('editmaxmark', 'quiz'),
]
)
);
return html_writer::span($output, 'instancemaxmarkcontainer');
$output = $this->output->render($structure->make_slot_maxmark_in_place_editable(
$structure->get_slot_id_for_slot($slot), $structure->get_context()));

return html_writer::span($output, 'instancemaxmarkcontainer instancemaxmark .mod_quiz_summarks newsummarks decimalplaces_' .
$structure->get_decimal_places_for_question_marks());
}

/**
Expand Down
24 changes: 24 additions & 0 deletions mod/quiz/classes/structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,26 @@ public function make_slot_display_number_in_place_editable(int $slotid, \context
get_string('edit_slotdisplaynumber_label', 'mod_quiz', $displayvalue));
}

/**
* Make slot maxmark in place editable api call.
* @param int $slotid
* @param \context $context
* @return \core\output\inplace_editable
*/
public function make_slot_maxmark_in_place_editable(int $slotid, \context $context): \core\output\inplace_editable {
$slot = $this->get_slot_by_id($slotid);
$editable = has_capability('mod/quiz:manage', $context);

// Get the current value.
$displayvalue = \html_writer::span(s($this->formatted_question_grade($slot->slot)),
'', ['data-sum-marks' => quiz_format_grade($this->get_quiz(), $this->get_quiz()->sumgrades)]);
return new inplace_editable('mod_quiz', 'slotmaxmark', $slotid,
$editable, $displayvalue, $slot->maxmark + 0,
get_string('editmaxmark', 'mod_quiz'),
get_string('editmaxmarkhint', 'mod_quiz', $displayvalue));
}

/**
* Get the page a given slot is on.
*
Expand Down Expand Up @@ -1120,6 +1140,10 @@ public function unset_question($slotid) {
public function update_slot_maxmark($slot, $maxmark) {
global $DB;

if (is_null($maxmark) || empty(trim($maxmark))) {
return false;
}

if (abs($maxmark - $slot->maxmark) < 1e-7) {
// Grade has not changed. Nothing to do.
return false;
Expand Down
1 change: 1 addition & 0 deletions mod/quiz/lang/en/quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
$string['editingquiz_link'] = 'mod/quiz/edit';
$string['editingquizx'] = 'Editing quiz: {$a}';
$string['editmaxmark'] = 'Edit maximum mark';
$string['editmaxmarkhint'] = 'New value for {$a}';
$string['editoverride'] = 'Edit override';
$string['editqcats'] = 'Edit questions categories';
$string['editquestion'] = 'Edit question';
Expand Down
34 changes: 25 additions & 9 deletions mod/quiz/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1179,23 +1179,39 @@ function quiz_review_option_form_to_db($fromform, $field) {
function mod_quiz_inplace_editable(string $itemtype, int $itemid, string $newvalue): \core\output\inplace_editable {
global $DB;

if ($itemtype === 'slotdisplaynumber') {
// Work out which quiz and slot this is.
$slot = $DB->get_record('quiz_slots', ['id' => $itemid], '*', MUST_EXIST);
$quizobj = quiz_settings::create($slot->quizid);
// Work out which quiz and slot this is.
$slot = $DB->get_record('quiz_slots', ['id' => $itemid], '*', MUST_EXIST);
$quizobj = quiz_settings::create($slot->quizid);

// Validate the context, and check the required capability.
$context = $quizobj->get_context();
\core_external\external_api::validate_context($context);
require_capability('mod/quiz:manage', $context);

// Validate the context, and check the required capability.
$context = $quizobj->get_context();
\core_external\external_api::validate_context($context);
require_capability('mod/quiz:manage', $context);
$structure = $quizobj->get_structure();

if ($itemtype === 'slotdisplaynumber') {
// Update the value - truncating the size of the DB column.
$structure = $quizobj->get_structure();
$structure->update_slot_display_number($itemid, core_text::substr($newvalue, 0, 16));

// Prepare the element for the output.
return $structure->make_slot_display_number_in_place_editable($itemid, $context);
}
if ($itemtype === 'slotmaxmark') {
if ($structure->update_slot_maxmark($slot, $newvalue)) {
// Grade has really changed.
$gradecalculator = $quizobj->get_grade_calculator();
quiz_delete_previews($quizobj->get_quiz());
$gradecalculator->recompute_quiz_sumgrades();
$gradecalculator->recompute_all_attempt_sumgrades();
$gradecalculator->recompute_all_final_grades();
quiz_update_grades($quizobj->get_quiz());
}

// Prepare the element for the output - reload structure to ensure totals are correct.
$structure = $quizobj->get_structure();
return $structure->make_slot_maxmark_in_place_editable($itemid, $context);
}
}

/**
Expand Down
7 changes: 3 additions & 4 deletions mod/quiz/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,8 @@ table.quizreviewsummary td.cell {
}

#page-mod-quiz-edit ul.slots li.section li.activity .instancemaxmarkcontainer {
background: white;
padding: 0.5em 0 0.5em 0.1em;
margin: 2px;
padding: 0;
margin: 0;
}

#page-mod-quiz-edit ul.slots li.section li.activity .instancemaxmarkcontainer .editicon {
Expand Down Expand Up @@ -1093,7 +1092,7 @@ table.quizreviewsummary td.cell {
background: #fdfdfe;
display: inline-block;
margin: 2px;
padding: 0.5em 0 0.5em 0.1em;
padding: 0.5em 0 0.5em;
}
#page-mod-quiz-edit .inplaceeditable.inplaceeditingon input {
width: 64px;
Expand Down
11 changes: 4 additions & 7 deletions mod/quiz/tests/behat/behat_mod_quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,10 @@ public function i_add_question_to_the_quiz_with($questiontype, $quizname, TableN
* @param string $newmark the mark to set
*/
public function i_set_the_max_mark_for_quiz_question($questionname, $newmark) {
$this->execute('behat_general::click_link', $this->escape(get_string('editmaxmark', 'quiz')));

$this->execute('behat_general::wait_until_exists', ["li input[name=maxmark]", "css_element"]);

$this->execute('behat_general::assert_page_contains_text', $this->escape(get_string('edittitleinstructions')));

$this->execute('behat_general::i_type', [$newmark]);
$this->execute('behat_forms::i_set_the_field_to',
[get_string('editmaxmark', 'mod_quiz'), $newmark]);
$this->execute('behat_general::i_press_named_key', ['', 'enter']);
// The press enter is needed again.
$this->execute('behat_general::i_press_named_key', ['', 'enter']);
}

Expand Down
Loading

0 comments on commit 7174977

Please sign in to comment.