Skip to content

Commit c98bc3b

Browse files
mkassaeiMahmoud Kassaei
authored andcommitted
MDL-53009 Quiz: Replace maxmark on Edit quiz page
with the inplace_editible element
1 parent dbf9b2b commit c98bc3b

15 files changed

Lines changed: 127 additions & 619 deletions

File tree

public/mod/quiz/classes/output/edit_renderer.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,8 @@ public function question(structure $structure, int $slot, \moodle_url $pageurl)
794794
'questionnumber' => $this->question_number($questionnumber, $structure->get_slot_by_number($slot)->defaultnumber),
795795
'questionname' => $this->get_question_name_for_slot($structure, $slot, $pageurl),
796796
'questionicons' => $this->get_action_icon($structure, $slot, $pageurl),
797+
'questionmaxmark' => $structure->make_slot_maxmark_in_place_editable($structure->get_slot_id_for_slot($slot),
798+
$structure->get_context()),
797799
'questiondependencyicon' => ($structure->can_be_edited() ? $this->question_dependency_icon($structure, $slot) : ''),
798800
'versionselection' => false,
799801
'draftversion' => $structure->get_question_in_slot($slot)->status == question_version_status::QUESTION_STATUS_DRAFT,
@@ -1175,22 +1177,12 @@ public function marked_out_of_field(structure $structure, $slot) {
11751177
return html_writer::span($output, 'instancemaxmarkcontainer infoitem');
11761178
}
11771179

1178-
$output = html_writer::span($structure->formatted_question_grade($slot),
1179-
'instancemaxmark decimalplaces_' . $structure->get_decimal_places_for_question_marks(),
1180-
['title' => get_string('maxmark', 'quiz')]);
1180+
$output = $this->output->render($structure->make_slot_maxmark_in_place_editable(
1181+
$structure->get_slot_id_for_slot($slot), $structure->get_context()));
11811182

1182-
$output .= html_writer::span(
1183-
html_writer::link(
1184-
new \moodle_url('#'),
1185-
$this->pix_icon('t/editstring', '', 'moodle', ['class' => 'editicon visibleifjs', 'title' => '']),
1186-
[
1187-
'class' => 'editing_maxmark',
1188-
'data-action' => 'editmaxmark',
1189-
'title' => get_string('editmaxmark', 'quiz'),
1190-
]
1191-
)
1192-
);
1193-
return html_writer::span($output, 'instancemaxmarkcontainer');
1183+
return html_writer::span($output,
1184+
'instancemaxmarkcontainer instancemaxmark .mod_quiz_summarks newsummarks decimalplaces_' .
1185+
$structure->get_decimal_places_for_question_marks());
11941186
}
11951187

11961188
/**

public/mod/quiz/classes/structure.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ public function make_slot_display_number_in_place_editable(int $slotid, \context
204204
get_string('edit_slotdisplaynumber_label', 'mod_quiz', $displayvalue));
205205
}
206206

207+
/**
208+
* Make slot maxmark in place editable api call.
209+
210+
* @param int $slotid
211+
* @param \context $context
212+
* @return \core\output\inplace_editable
213+
*/
214+
public function make_slot_maxmark_in_place_editable(int $slotid, \context $context): \core\output\inplace_editable {
215+
$slot = $this->get_slot_by_id($slotid);
216+
$editable = has_capability('mod/quiz:manage', $context);
217+
218+
// Get the current value.
219+
$displayvalue = \html_writer::span(s($this->formatted_question_grade($slot->slot)),
220+
'', ['data-sum-marks' => quiz_format_grade($this->get_quiz(), $this->get_quiz()->sumgrades)]);
221+
return new inplace_editable('mod_quiz', 'slotmaxmark', $slotid,
222+
$editable, $displayvalue, $slot->maxmark + 0,
223+
get_string('editmaxmark', 'mod_quiz'),
224+
get_string('editmaxmarkhint', 'mod_quiz', $displayvalue));
225+
}
226+
207227
/**
208228
* Get the page a given slot is on.
209229
*
@@ -1158,6 +1178,10 @@ public function unset_question($slotid) {
11581178
public function update_slot_maxmark($slot, $maxmark) {
11591179
global $DB;
11601180

1181+
if (is_null($maxmark) || empty(trim($maxmark))) {
1182+
return false;
1183+
}
1184+
11611185
if (abs($maxmark - $slot->maxmark) < 1e-7) {
11621186
// Grade has not changed. Nothing to do.
11631187
return false;

public/mod/quiz/edit_rest.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,6 @@
120120
$result = ['visible' => true];
121121
break;
122122

123-
case 'getmaxmark':
124-
require_capability('mod/quiz:manage', $modcontext);
125-
$slot = $DB->get_record('quiz_slots', ['id' => $id], '*', MUST_EXIST);
126-
$result = ['instancemaxmark' => quiz_format_question_grade($quiz, $slot->maxmark)];
127-
break;
128-
129-
case 'updatemaxmark':
130-
require_capability('mod/quiz:manage', $modcontext);
131-
$slot = $structure->get_slot_by_id($id);
132-
if ($structure->update_slot_maxmark($slot, $maxmark)) {
133-
// Grade has really changed.
134-
quiz_delete_previews($quiz);
135-
$gradecalculator->recompute_quiz_sumgrades();
136-
$gradecalculator->recompute_all_attempt_sumgrades();
137-
$gradecalculator->recompute_all_final_grades();
138-
quiz_update_grades($quiz, 0, true);
139-
}
140-
$result = ['instancemaxmark' => quiz_format_question_grade($quiz, $maxmark),
141-
'newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades)];
142-
break;
143-
144123
case 'updatepagebreak':
145124
require_capability('mod/quiz:manage', $modcontext);
146125
$slots = $structure->update_page_break($id, $value);

public/mod/quiz/lang/en/quiz.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@
304304
$string['editingquiz_link'] = 'mod/quiz/edit';
305305
$string['editingquizx'] = 'Editing quiz: {$a}';
306306
$string['editmaxmark'] = 'Edit maximum mark';
307+
$string['editmaxmarkhint'] = 'New value for {$a}';
307308
$string['editoverride'] = 'Edit override';
308309
$string['editqcats'] = 'Edit questions categories';
309310
$string['editquestion'] = 'Edit question';

public/mod/quiz/lib.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,33 +1095,49 @@ function quiz_review_option_form_to_db($fromform, $field) {
10951095
}
10961096

10971097
/**
1098-
* In place editable callback for slot displaynumber.
1098+
* In place editable callback to quiz slots.
10991099
*
1100-
* @param string $itemtype slotdisplarnumber
1100+
* @param string $itemtype the slot type
11011101
* @param int $itemid the id of the slot in the quiz_slots table
1102-
* @param string $newvalue the new value for displaynumber field for a given slot in the quiz_slots table
1102+
* @param string $newvalue the new value for a given slot in the quiz_slots table
11031103
* @return \core\output\inplace_editable|void
11041104
*/
11051105
function mod_quiz_inplace_editable(string $itemtype, int $itemid, string $newvalue): \core\output\inplace_editable {
11061106
global $DB;
11071107

1108-
if ($itemtype === 'slotdisplaynumber') {
1109-
// Work out which quiz and slot this is.
1110-
$slot = $DB->get_record('quiz_slots', ['id' => $itemid], '*', MUST_EXIST);
1111-
$quizobj = quiz_settings::create($slot->quizid);
1108+
// Work out which quiz and slot this is.
1109+
$slot = $DB->get_record('quiz_slots', ['id' => $itemid], '*', MUST_EXIST);
1110+
$quizobj = quiz_settings::create($slot->quizid);
1111+
1112+
// Validate the context, and check the required capability.
1113+
$context = $quizobj->get_context();
1114+
\core_external\external_api::validate_context($context);
1115+
require_capability('mod/quiz:manage', $context);
11121116

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

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

11221123
// Prepare the element for the output.
11231124
return $structure->make_slot_display_number_in_place_editable($itemid, $context);
11241125
}
1126+
if ($itemtype === 'slotmaxmark') {
1127+
if ($structure->update_slot_maxmark($slot, $newvalue)) {
1128+
// Grade has really changed.
1129+
$gradecalculator = $quizobj->get_grade_calculator();
1130+
quiz_delete_previews($quizobj->get_quiz());
1131+
$gradecalculator->recompute_quiz_sumgrades();
1132+
$gradecalculator->recompute_all_attempt_sumgrades();
1133+
$gradecalculator->recompute_all_final_grades();
1134+
quiz_update_grades($quizobj->get_quiz());
1135+
}
1136+
1137+
// Prepare the element for the output - reload structure to ensure totals are correct.
1138+
$structure = $quizobj->get_structure();
1139+
return $structure->make_slot_maxmark_in_place_editable($itemid, $context);
1140+
}
11251141
}
11261142

11271143
/**

public/mod/quiz/styles.css

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,8 @@ table.quizreviewsummary td.cell {
664664
}
665665

666666
#page-mod-quiz-edit ul.slots li.section li.activity .instancemaxmarkcontainer {
667-
background: white;
668-
padding: 0.5em 0 0.5em 0.1em;
669-
margin: 2px;
667+
padding: 0;
668+
margin: 0;
670669
}
671670

672671
#page-mod-quiz-edit ul.slots li.section li.activity .instancemaxmarkcontainer .editicon {
@@ -677,10 +676,6 @@ table.quizreviewsummary td.cell {
677676
background: transparent;
678677
}
679678

680-
#page-mod-quiz-edit ul.slots li.section li.activity .instancemaxmarkcontainer form {
681-
display: inline;
682-
}
683-
684679
#page-mod-quiz-edit ul.slots li.section li.activity .instancemaxmark {
685680
display: inline-block;
686681
text-align: right;
@@ -1093,7 +1088,7 @@ table.quizreviewsummary td.cell {
10931088
background: #fdfdfe;
10941089
display: inline-block;
10951090
margin: 2px;
1096-
padding: 0.5em 0 0.5em 0.1em;
1091+
padding: 0.5em 0 0.5em;
10971092
}
10981093
#page-mod-quiz-edit .inplaceeditable.inplaceeditingon input {
10991094
width: 64px;

public/mod/quiz/tests/behat/behat_mod_quiz.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,10 @@ public function i_add_question_to_the_quiz_with($questiontype, $quizname, TableN
475475
* @param string $newmark the mark to set
476476
*/
477477
public function i_set_the_max_mark_for_quiz_question($questionname, $newmark) {
478-
$this->execute('behat_general::click_link', $this->escape(get_string('editmaxmark', 'quiz')));
479-
480-
$this->execute('behat_general::wait_until_exists', ["li input[name=maxmark]", "css_element"]);
481-
482-
$this->execute('behat_general::assert_page_contains_text', $this->escape(get_string('edittitleinstructions')));
483-
484-
$this->execute('behat_general::i_type', [$newmark]);
478+
$this->execute('behat_forms::i_set_the_field_to',
479+
[get_string('editmaxmark', 'mod_quiz'), $newmark]);
480+
$this->execute('behat_general::i_press_named_key', ['', 'enter']);
481+
// The press enter is needed again.
485482
$this->execute('behat_general::i_press_named_key', ['', 'enter']);
486483
}
487484

0 commit comments

Comments
 (0)