moodle_local_treestudyplan/classes/teachingfinder.php
2023-08-27 17:00:23 +02:00

133 lines
4.7 KiB
PHP

<?php
// 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/>.
/**
* Class to find and cache which studyplans a teacher is teaching courses in
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan;
/**
* Class to find and cache which studyplans a teacher is teaching courses in
*/
class teachingfinder {
/**
* Table name used for caching teacher info
* @var string
*/
const TABLE = "local_treestudyplan_teachers";
/**
* List all studyplans the vurrent user is teaching
* @return studyplan[] List of studyplans
*/
public static function list_my_plans() {
global $USER, $DB;
$userid = $USER->id;
$records = $DB->get_records(self::TABLE, ['teacher_id' => $userid]);
if (count($records) == 0) {
// Initiate a search if the cache is empty.
self::update_teaching_cache($userid);
$DB->get_records(self::TABLE, ['teacher_id' => $userid]);
}
$list = [];
foreach ($records as $r) {
$list[] = studyplan::find_by_id($r->studyplan_id);
}
return $list;
}
/**
* Find The active studyplan pages where the specified user is a teacher
* (Has the mod/assign::grade capability in one of the linked courses)
* @param int $userid The userid to check teacher rights for
* @return int[] List of page id's a teacher is teaching courses in
*/
public static function update_teaching_cache($userid) {
global $DB;
$list = [];
// First find all active study plans.
$sql = "SELECT p.id FROM {local_treestudyplan_page} p
WHERE startdate <= NOW() and enddate >= NOW()";
$pageids = $DB->get_fieldset_sql($sql, []);
// Then parse them to see if the user has the grading permission in any of them .
// (Which would make them a teacher for all intents and purposes).
foreach ($pageids as $pageid) {
$sql = "SELECT i.course_id FROM {local_treestudyplan_item} i
INNER JOIN {local_treestudyplan_line} l ON i.line_id = l.id
WHERE l.page_id = :page_id AND i.course_id IS NOT NULL";
$courseids = $DB->get_fieldset_sql($sql, ["page_id" => $pageid]);
$linked = false;
foreach ($courseids as $cid) {
$coursecontext = \context_course::instance($cid);
if (is_enrolled($coursecontext, $userid, 'mod/assign:grade')) {
$linked = true;
break; // No need to search further.
}
}
if ($linked) {
$list[] = $pageid;
}
}
// Now, clear the database of all records for this user.
$DB->delete_records(self::TABLE, ["teacher_id" => $userid]);
// And add new records for the found studyplans.
$now = time();
foreach ($list as $pageid) {
// Retrieve the studyplan id from the page.
// TODO: Change this when page management is implemented to return the page instead of the plan.
$planid = $DB->get_field("local_treestudyplan_page", "studyplan_id", ["id" => $pageid]);
$DB->insert_record(self::TABLE, ["teacher_id" => $userid, "studyplan_id" => $planid, "update_time" => $now]);
}
return $list;
}
/**
* List of recognized teacher id's
* @return intp[]
*/
public static function list_teacher_ids() {
global $DB;
return $DB->get_fieldset_sql("SELECT DISTINCT teacher_id FROM {".self::TABLE."}");
}
/**
* Get the last time of update for the specified teacher
* @param int $teacherid User id of teacher
* @return int Unix timestamp
*/
public static function get_update_time($teacherid): int {
global $DB;
$r = $DB->get_field_sql("SELECT MIN(update_time) FROM {".self::TABLE."} WHERE teacher_id=:teacher_id",
["teacher_id" => $teacherid]);
return (int)($r->update_time);
}
}