From 912b5d9713aa006299b94b7697995f08403e6343 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 25 Mar 2024 04:15:58 +0100 Subject: [PATCH] Code cleanup and fixing CI errors --- .github/workflows/moodle-ci.yml | 2 +- ajax.php | 246 +++++++++--------- amd/build.sh | 11 - .../backup_jazzquiz_activity_task.class.php | 4 + backup/moodle2/backup_jazzquiz_stepslib.php | 24 +- .../restore_jazzquiz_activity_task.class.php | 8 +- backup/moodle2/restore_jazzquiz_stepslib.php | 9 +- classes/bank/add_questions_bank_view.php | 57 ++++ classes/bank/jazzquiz_question_bank_view.php | 213 --------------- ...ion_bank_add_to_jazzquiz_action_column.php | 68 ----- classes/exporter.php | 30 ++- classes/forms/view/start_session.php | 23 +- classes/improviser.php | 134 +++++----- classes/jazzquiz.php | 183 +++++++------ classes/jazzquiz_attempt.php | 227 ++++++++++------ classes/jazzquiz_question.php | 20 +- classes/jazzquiz_session.php | 223 ++++++++-------- classes/jazzquiz_vote.php | 28 +- classes/local/page_requirements_diff.php | 79 ++++++ classes/output/renderer.php | 196 +++++--------- classes/privacy/provider.php | 54 ++-- db/access.php | 51 ++-- db/upgrade.php | 9 +- edit.php | 84 ++---- index.php | 12 +- lang/en/jazzquiz.php | 3 +- lib.php | 46 ++-- locallib.php | 11 +- reports.php | 26 +- styles.css | 44 +--- templates/continue_session.mustache | 29 ++- templates/edit_question_list.mustache | 126 +++++---- templates/guests_not_allowed.mustache | 30 ++- templates/join_session.mustache | 44 +++- templates/no_session.mustache | 31 ++- templates/question.mustache | 43 ++- templates/quiz.mustache | 58 ++++- templates/report.mustache | 147 ++++++++--- templates/start_session.mustache | 31 ++- view.php | 75 ++---- 40 files changed, 1376 insertions(+), 1363 deletions(-) delete mode 100755 amd/build.sh create mode 100644 classes/bank/add_questions_bank_view.php delete mode 100755 classes/bank/jazzquiz_question_bank_view.php delete mode 100755 classes/bank/question_bank_add_to_jazzquiz_action_column.php create mode 100644 classes/local/page_requirements_diff.php diff --git a/.github/workflows/moodle-ci.yml b/.github/workflows/moodle-ci.yml index 070b8b2..1c0819b 100644 --- a/.github/workflows/moodle-ci.yml +++ b/.github/workflows/moodle-ci.yml @@ -98,7 +98,7 @@ jobs: - name: Grunt if: ${{ !cancelled() }} - run: moodle-plugin-ci grunt --max-lint-warnings 0 + run: moodle-plugin-ci grunt --max-lint-warnings 11 # For now just make sure we don't get more warnings. - name: PHPUnit tests if: ${{ !cancelled() }} diff --git a/ajax.php b/ajax.php index 416a4c5..1429ead 100755 --- a/ajax.php +++ b/ajax.php @@ -27,6 +27,8 @@ namespace mod_jazzquiz; +use question_bank; + define('AJAX_SCRIPT', true); require_once('../../config.php'); @@ -39,18 +41,17 @@ /** * Send a list of all the questions tagged for use with improvisation. + * * @param jazzquiz $jazzquiz - * @return mixed[] + * @return array */ -function show_all_improvise_questions(jazzquiz $jazzquiz) { +function show_all_improvise_questions(jazzquiz $jazzquiz): array { $improviser = new improviser($jazzquiz); $questionrecords = $improviser->get_all_improvised_question_definitions(); - debugging( 'show'.$questionrecords ); if (!$questionrecords) { - debugging( 'No improvisation questions' ); return [ 'status' => 'error', - 'message' => 'No improvisation questions' + 'message' => 'No improvisation questions', ]; } $questions = []; @@ -59,21 +60,22 @@ function show_all_improvise_questions(jazzquiz $jazzquiz) { 'questionid' => $question->id, 'jazzquizquestionid' => 0, 'name' => str_replace('{IMPROV}', '', $question->name), - 'time' => $jazzquiz->data->defaultquestiontime + 'time' => $jazzquiz->data->defaultquestiontime, ]; } return [ 'status' => 'success', - 'questions' => $questions + 'questions' => $questions, ]; } /** * Send a list of all the questions added to the quiz. + * * @param jazzquiz $jazzquiz - * @return mixed[] + * @return array */ -function show_all_jump_questions(jazzquiz $jazzquiz) { +function show_all_jump_questions(jazzquiz $jazzquiz): array { global $DB; $sql = 'SELECT q.id AS id, q.name AS name, jq.questiontime AS time, jq.id AS jqid'; $sql .= ' FROM {jazzquiz_questions} jq'; @@ -87,21 +89,22 @@ function show_all_jump_questions(jazzquiz $jazzquiz) { 'questionid' => $question->id, 'jazzquizquestionid' => $question->jqid, 'name' => $question->name, - 'time' => $question->time + 'time' => $question->time, ]; } return [ 'status' => 'success', - 'questions' => $questions + 'questions' => $questions, ]; } /** * Get the form for the current question. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function get_question_form(jazzquiz_session $session) { +function get_question_form(jazzquiz_session $session): array { $session->load_session_questions(); $slot = optional_param('slot', 0, PARAM_INT); if ($slot === 0) { @@ -111,12 +114,12 @@ function get_question_form(jazzquiz_session $session) { $js = ''; $css = []; $isalreadysubmitted = true; - if (!$session->attempt->has_responded($slot)) { + if (!$session->myattempt->has_responded($slot)) { $jazzquiz = $session->jazzquiz; /** @var output\renderer $renderer */ $renderer = $jazzquiz->renderer; $isinstructor = $jazzquiz->is_instructor(); - list($html, $js, $css) = $renderer->render_question_form($slot, $session->attempt, $jazzquiz, $isinstructor); + list($html, $js, $css) = $renderer->render_question_form($slot, $session->myattempt, $jazzquiz, $isinstructor); $isalreadysubmitted = false; } $qtype = $session->get_question_type_by_slot($slot); @@ -127,16 +130,17 @@ function get_question_form(jazzquiz_session $session) { 'css' => $css, 'question_type' => $qtype, 'is_already_submitted' => $isalreadysubmitted, - 'voteable' => !in_array($qtype, $voteable) + 'voteable' => !in_array($qtype, $voteable), ]; } /** * Start a new question. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function start_question(jazzquiz_session $session) { +function start_question(jazzquiz_session $session): array { $session->load_session_questions(); $session->load_attempts(); $method = required_param('method', PARAM_ALPHA); @@ -148,7 +152,9 @@ function start_question(jazzquiz_session $session) { $jazzquizquestionid = optional_param('jazzquizquestionid', 0, PARAM_INT); if ($jazzquizquestionid !== 0) { $jazzquizquestion = $session->jazzquiz->get_question_by_id($jazzquizquestionid); - $session->data->slot = $jazzquizquestion->data->slot; + if ($jazzquizquestion) { + $session->data->slot = $jazzquizquestion->data->slot; + } } break; case 'repoll': @@ -156,7 +162,7 @@ function start_question(jazzquiz_session $session) { if ($lastslot === 0) { return [ 'status' => 'error', - 'message' => 'Nothing to repoll.' + 'message' => 'Nothing to repoll.', ]; } $questionid = $session->questions[$lastslot]->questionid; @@ -167,7 +173,7 @@ function start_question(jazzquiz_session $session) { if ($session->data->slot >= $lastslot) { return [ 'status' => 'error', - 'message' => 'No next question.' + 'message' => 'No next question.', ]; } $session->data->slot++; @@ -190,7 +196,7 @@ function start_question(jazzquiz_session $session) { default: return [ 'status' => 'error', - 'message' => "Invalid method $method" + 'message' => "Invalid method $method", ]; } list($success, $questiontime) = $session->start_question($questionid, $questiontime); @@ -207,20 +213,21 @@ function start_question(jazzquiz_session $session) { return [ 'status' => 'success', 'questiontime' => $questiontime, - 'delay' => $session->data->nextstarttime - time() + 'delay' => $session->data->nextstarttime - time(), ]; } /** * Start the quiz. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function start_quiz(jazzquiz_session $session) { +function start_quiz(jazzquiz_session $session): array { if ($session->data->status !== 'notrunning') { return [ 'status' => 'error', - 'message' => 'Quiz is already running' + 'message' => 'Quiz is already running', ]; } $session->data->status = 'preparing'; @@ -230,15 +237,16 @@ function start_quiz(jazzquiz_session $session) { /** * Submit a response for the current question. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function save_question(jazzquiz_session $session) { - $attempt = $session->attempt; +function save_question(jazzquiz_session $session): array { + $attempt = $session->myattempt; if (!$attempt->belongs_to_current_user()) { return [ 'status' => 'error', - 'message' => 'Invalid user' + 'message' => 'Invalid user', ]; } $session->load_session_questions(); @@ -251,22 +259,23 @@ function save_question(jazzquiz_session $session) { } return [ 'status' => 'success', - 'feedback' => $feedback + 'feedback' => $feedback, ]; } /** * Start a vote. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function run_voting(jazzquiz_session $session) { +function run_voting(jazzquiz_session $session): array { $questions = required_param('questions', PARAM_RAW); $questions = json_decode(urldecode($questions), true); if (!$questions) { return [ 'status' => 'error', - 'message' => 'Failed to decode questions' + 'message' => 'Failed to decode questions', ]; } $qtype = optional_param('question_type', '', PARAM_ALPHANUM); @@ -283,10 +292,11 @@ function run_voting(jazzquiz_session $session) { /** * Save a vote. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function save_vote(jazzquiz_session $session) { +function save_vote(jazzquiz_session $session): array { $voteid = required_param('vote', PARAM_INT); $vote = new jazzquiz_vote($session->data->id); $status = $vote->save_vote($voteid); @@ -296,9 +306,9 @@ function save_vote(jazzquiz_session $session) { /** * Get the vote results. * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function get_vote_results(jazzquiz_session $session) { +function get_vote_results(jazzquiz_session $session): array { $session->load_session_questions(); $session->load_attempts(); $slot = count($session->questions); @@ -306,16 +316,17 @@ function get_vote_results(jazzquiz_session $session) { $votes = $vote->get_results(); return [ 'answers' => $votes, - 'total_students' => $session->get_student_count() + 'total_students' => $session->get_student_count(), ]; } /** * End the current question. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function end_question($session) { +function end_question(jazzquiz_session $session): array { $session->data->status = 'reviewing'; $session->save(); return ['status' => 'success']; @@ -323,20 +334,22 @@ function end_question($session) { /** * Get the correct answer for the current question. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function get_right_response(jazzquiz_session $session) { +function get_right_response(jazzquiz_session $session): array { $session->load_session_questions(); return ['right_answer' => $session->get_question_right_response()]; } /** * Close a session. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function close_session(jazzquiz_session $session) { +function close_session(jazzquiz_session $session): array { $session->load_attempts(); $session->end_session(); return ['status' => 'success']; @@ -344,10 +357,11 @@ function close_session(jazzquiz_session $session) { /** * Get the results for a session. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function get_results(jazzquiz_session $session) { +function get_results(jazzquiz_session $session): array { $session->load_session_questions(); $session->load_attempts(); $slot = count($session->questions); @@ -365,16 +379,17 @@ function get_results(jazzquiz_session $session) { 'responses' => $results['responses'], 'responded' => $results['responded'], 'total_students' => $results['student_count'], - 'merge_count' => $mergecount + 'merge_count' => $mergecount, ]; } /** * Merge a response into another. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function merge_responses(jazzquiz_session $session) { +function merge_responses(jazzquiz_session $session): array { $session->load_session_questions(); $slot = optional_param('slot', count($session->questions), PARAM_INT); if (!isset($session->questions[$slot])) { @@ -388,10 +403,11 @@ function merge_responses(jazzquiz_session $session) { /** * Undo the last merge. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function undo_merge(jazzquiz_session $session) { +function undo_merge(jazzquiz_session $session): array { $session->load_session_questions(); $slot = optional_param('slot', count($session->questions), PARAM_INT); if (!isset($session->questions[$slot])) { @@ -403,9 +419,10 @@ function undo_merge(jazzquiz_session $session) { /** * Convert STACK (Maxima) string to LaTeX format. - * @return mixed[] + * + * @return array */ -function stack_to_latex() { +function stack_to_latex(): array { global $DB; $input = required_param('input', PARAM_RAW); $input = urldecode($input); @@ -414,28 +431,29 @@ function stack_to_latex() { return [ 'message' => 'STACK question not found.', 'latex' => $input, - 'original' => $input + 'original' => $input, ]; } /** @var \qtype_stack_question $question */ - $question = \question_bank::load_question($question->id); + $question = question_bank::load_question($question->id); $question->initialise_question_from_seed(); $state = $question->get_input_state('ans1', ['ans1' => $input]); $latex = $state->contentsdisplayed; return [ 'latex' => $latex, - 'original' => $input + 'original' => $input, ]; } /** * Retrieve the current state of the session. + * * @param jazzquiz_session $session - * @return mixed[] + * @return array */ -function session_info(jazzquiz_session $session) { +function session_info(jazzquiz_session $session): array { global $DB; switch ($session->data->status) { // Just a generic response with the state. @@ -445,7 +463,7 @@ function session_info(jazzquiz_session $session) { $session->load_attempts(); return [ 'status' => $session->data->status, - 'student_count' => $session->get_student_count() + 'student_count' => $session->get_student_count(), ]; } // Fall-through. @@ -453,7 +471,7 @@ function session_info(jazzquiz_session $session) { case 'reviewing': return [ 'status' => $session->data->status, - 'slot' => $session->data->slot // For the preplanned questions. + 'slot' => $session->data->slot, // For the preplanned questions. ]; case 'voting': @@ -466,7 +484,7 @@ function session_info(jazzquiz_session $session) { 'text' => $voteoption->attempt, 'id' => $voteoption->id, 'question_type' => $voteoption->qtype, - 'content_id' => "vote_answer_label_$i" + 'content_id' => "vote_answer_label_$i", ]; $html .= '