moodle_local_treestudyplan/classes/local/ungradedscanners/quiz_scanner.php

110 lines
4.2 KiB
PHP
Raw Permalink Normal View History

<?php
2023-08-24 23:02:41 +02:00
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
2023-08-27 15:12:54 +02:00
* Scan submissions in assign activities
2023-08-24 23:02:41 +02:00
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan\local\ungradedscanners;
2023-08-25 17:33:20 +02:00
defined('MOODLE_INTERNAL') || die();
2023-08-25 10:41:56 +02:00
require_once($CFG->dirroot.'/question/engine/states.php'); // For reading question state.
2023-08-27 15:12:54 +02:00
/**
* Activity scanner for quiz activity
*/
class quiz_scanner extends scanner_base {
2023-08-27 15:12:54 +02:00
/**
* Retrieve the ungraded submissions in this activity
*/
2023-08-24 23:02:41 +02:00
protected function get_ungraded_submissions() {
2023-08-25 09:44:34 +02:00
// Count all users who have one or more questions that still need grading.
global $DB;
2023-08-24 23:02:41 +02:00
// First find all question attempts that need grading.
2023-08-25 17:33:20 +02:00
$sql = "SELECT qza.id as submissionid, qza.userid as userid,
qas.questionattemptid as attempt_id, qas.sequencenumber as sequencenumber
FROM {question_attempt_steps} qas
JOIN {question_attempts} qna ON qas.questionattemptid = qna.id
JOIN {quiz_attempts} qza ON qna.questionusageid = qza.uniqueid
WHERE qas.state = 'needsgrading' AND qza.quiz = :iteminstance";
2023-08-26 16:51:35 +02:00
$rs = $DB->get_recordset_sql($sql, ['iteminstance' => $this->gi->iteminstance]);
$submissions = [];
2023-08-24 23:02:41 +02:00
foreach ($rs as $r) {
// Now, check if .
2023-08-25 17:33:20 +02:00
$maxstatesql = "SELECT MAX(qas.sequencenumber)
FROM {question_attempt_steps} qas
WHERE qas.questionattemptid = {$r->attempt_id}";
2023-08-25 09:33:42 +02:00
$max = $DB->get_field_sql($maxstatesql);
2023-08-24 23:02:41 +02:00
if ($r->sequencenumber == $max) {
2023-08-25 10:41:56 +02:00
$submissions[$r->userid] = true; // Set array index based on user id, to avoid checking if value is in array.
}
}
$rs->close();
return array_keys($submissions);
}
2023-08-27 15:12:54 +02:00
/**
* Count the number of students wit ungraded submissions in this activity
* @param array $courseuserids
* @return int Number of students with ungraded submissions
*/
2023-08-25 13:04:19 +02:00
public function count_ungraded($courseuserids = []) {
$ungraded = $this->get_ungraded_submissions();
2023-08-25 09:33:42 +02:00
if (count($courseuserids) > 0) {
$ungraded = array_intersect($ungraded, $courseuserids);
}
return count($ungraded);
}
2023-08-27 15:12:54 +02:00
/**
* Count the number of students wit (all) graded submissions in this activity
* @param array $courseuserids
* @return int Number of students with graded submission
*/
2023-08-25 13:04:19 +02:00
public function count_graded($courseuserids = []) {
2023-08-25 09:44:34 +02:00
// Count all users who submitted one or more finished tests.
global $DB;
2023-08-24 23:02:41 +02:00
$sql = "SELECT DISTINCT g.userid
FROM {grade_grades} g
LEFT JOIN {grade_items} gi on g.itemid = gi.id
WHERE gi.itemmodule = 'quiz' AND gi.iteminstance = :iteminstance
2023-08-24 23:02:41 +02:00
AND g.finalgrade IS NOT NULL"; // MAy turn out to be needed, dunno.
2023-08-26 16:51:35 +02:00
$graded = $DB->get_fieldset_sql($sql, ['iteminstance' => $this->gi->iteminstance]);
2023-08-24 23:02:41 +02:00
2023-08-25 09:33:42 +02:00
if (count($courseuserids) > 0) {
$graded = array_intersect($graded, $courseuserids);
}
2023-08-24 23:02:41 +02:00
return count($graded);
}
2023-08-27 15:12:54 +02:00
/**
* Check if specified user has ungraded submission in this activity
* @param mixed $userid User id of user to check
* @return bool If specified user has ungraded submissions in this activity
*/
2023-08-24 23:02:41 +02:00
public function has_ungraded_submission($userid) {
$ungraded = $this->get_ungraded_submissions();
2023-08-24 23:02:41 +02:00
return in_array($userid, $ungraded);
}
2023-08-25 17:33:20 +02:00
}