moodle_local_treestudyplan/classes/completion.php

108 lines
3.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/>.
2024-07-19 12:31:26 +02:00
2023-08-24 23:02:41 +02:00
/**
2023-08-27 23:38:16 +02:00
* Completion model
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;
2023-08-25 12:04:27 +02:00
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/externallib.php');
2023-08-27 23:38:16 +02:00
/**
* Model of different completion states
*/
class completion {
2023-08-27 23:38:16 +02:00
/** @var int */
public const FAILED = -1;
2023-08-27 23:38:16 +02:00
/** @var int */
public const INCOMPLETE = 0;
2023-08-27 23:38:16 +02:00
/** @var int */
public const PENDING = 1;
2023-08-27 23:38:16 +02:00
/** @var int */
public const PROGRESS = 2;
2023-08-27 23:38:16 +02:00
/** @var int */
public const COMPLETED = 3;
2023-08-27 23:38:16 +02:00
/** @var int */
public const GOOD = 4;
2023-08-27 23:38:16 +02:00
/** @var int */
public const EXCELLENT = 5;
2023-08-27 23:38:16 +02:00
/**
* Map of completion state constants to string labels
* @var array
*/
private const LABELS = [
self::FAILED => 'failed',
self::INCOMPLETE => 'incomplete',
self::PENDING => 'pending',
self::PROGRESS => 'progress',
self::COMPLETED => 'completed',
self::GOOD => 'good',
self::EXCELLENT => 'excellent',
];
2023-08-27 23:38:16 +02:00
/**
* Convert completion state to equivalent text label
* @param int $completion
* @return string
*/
public static function label($completion) {
2023-08-24 23:02:41 +02:00
if (array_key_exists($completion, self::LABELS)) {
return self::LABELS[$completion];
2023-08-25 09:33:42 +02:00
} else {
return self::LABELS[self::INCOMPLETE];
}
}
2023-08-27 23:38:16 +02:00
/**
* Webservice structure for basic info
* @param int $value Webservice requirement constant
*/
2024-06-02 19:23:40 +02:00
public static function structure($value = VALUE_REQUIRED): \external_description {
2023-08-25 11:52:05 +02:00
return new \external_value( PARAM_TEXT,
'completion state (failed|incomplete|pending|progress|completed|good|excellent)',
$value);
}
2023-08-27 23:38:16 +02:00
/**
* Count how many times each state occurs in a given array of completion states
* @param int[] $states The array of completion states
* @return array of state counts
*/
2023-08-24 23:02:41 +02:00
public static function count_states(array $states) {
2023-08-25 09:44:34 +02:00
// Initialize result array.
$statecount = [];
2023-08-24 23:02:41 +02:00
foreach (array_keys(self::LABELS) as $key) {
$statecount[$key] = 0;
}
2023-08-25 09:44:34 +02:00
// Process all states in array and increment relevant counter for each one.
2023-08-24 23:02:41 +02:00
foreach ($states as $c) {
if (array_key_exists($c, $statecount)) {
$statecount[$c] += 1;
}
}
return $statecount;
}
2023-08-25 11:52:05 +02:00
}