moodle_local_treestudyplan/classes/teachingfinder.php

199 lines
6.9 KiB
PHP
Raw 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 17:00:23 +02:00
* Class to find and cache which studyplans a teacher is teaching courses in
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-27 17:00:23 +02:00
/**
* Class to find and cache which studyplans a teacher is teaching courses in
*/
class teachingfinder {
2023-08-27 22:20:17 +02:00
/**
2023-08-27 17:00:23 +02:00
* Table name used for caching teacher info
* @var string
*/
2023-08-27 21:23:39 +02:00
/** @var string */
const TABLE = "local_treestudyplan_teachers";
2023-08-27 17:00:23 +02:00
/**
* List all studyplans the current user is teaching
* (Updates the cache if no results are found the first time)
2023-08-27 17:00:23 +02:00
* @return studyplan[] List of studyplans
*/
2023-08-24 23:02:41 +02:00
public static function list_my_plans() {
global $USER, $DB;
$userid = $USER->id;
2023-08-24 23:02:41 +02:00
$records = $DB->get_records(self::TABLE, ['teacher_id' => $userid]);
if (count($records) == 0) {
2023-08-25 09:44:34 +02:00
// Initiate a search if the cache is empty.
self::update_teaching_cache($userid);
$records = $DB->get_records(self::TABLE, ['teacher_id' => $userid]);
}
$list = [];
2023-08-24 23:02:41 +02:00
foreach ($records as $r) {
2023-08-25 17:33:20 +02:00
$list[] = studyplan::find_by_id($r->studyplan_id);
}
return $list;
}
/**
* Check if a user is teaching in a specific studyplan
* (Does not update the cache if results are 0)
* @param studyplan $plan Studyplan to check
2024-06-03 04:00:46 +02:00
* @param int $userid UserId to check for
* @return bool If teaching in this plan
*/
2024-06-02 19:23:40 +02:00
public static function is_teaching_studyplan(studyplan $plan, $userid) {
global $DB;
$count = $DB->count_records(self::TABLE, ['teacher_id' => $userid, "studyplan_id" => $plan->id()]);
2024-06-02 23:23:32 +02:00
return ($count > 0) ? true : false;
}
/**
* Check if a user is teaching courses in any studyplan
* (Does not update the cache if results are 0)
2024-06-03 04:00:46 +02:00
* @param int $userid UserId to check for
* @return bool If teaching in any plan
*/
public static function is_teaching($userid) {
global $DB;
$count = $DB->count_records(self::TABLE, ['teacher_id' => $userid]);
2024-06-02 23:23:32 +02:00
return ($count > 0) ? true : false;
}
/**
* Check if current user is teaching in a specific studyplan
* (Does not update the cache if results are 0)
* @param studyplan $plan Studyplan to check
* @return bool If teaching in this plan
*/
public static function am_teaching_studyplan(studyplan $plan) {
global $USER;
2024-06-02 19:23:40 +02:00
return self::is_teaching_studyplan($plan, $USER->id);
}
/**
* Check if current user is teaching courses in any studyplan
* (Does not update the cache if results are 0)
2024-06-03 04:00:46 +02:00
* @return bool If teaching in any studyplan
*/
public static function am_teaching() {
global $USER;
return self::is_teaching($USER->id);
}
2024-06-02 19:23:40 +02:00
/**
* Check if a user is teaching in a specific course
* @param int $courseid ID of the course
* @param int $userid ID of the user
* @return bool True if teaching in the course
*/
public static function is_teaching_course($courseid, $userid) {
$coursecontext = \context_course::instance($courseid);
return is_enrolled($coursecontext, $userid, 'mod/assign:grade');
}
2024-06-02 19:23:40 +02:00
/**
* Check if current user is teaching in a specific course
* @param int $courseid ID of the course
* @return bool True if teaching in the course
*/
public static function am_teaching_course($courseid) {
global $USER;
2024-06-02 19:23:40 +02:00
return self::is_teaching_course($courseid, $USER->id);
}
/**
2023-08-27 17:00:23 +02:00
* Find The active studyplan pages where the specified user is a teacher
* (Has the mod/assign::grade capability in one of the linked courses)
2023-08-27 17:00:23 +02:00
* @param int $userid The userid to check teacher rights for
* @return int[] List of page id's a teacher is teaching courses in
*/
2023-08-24 23:02:41 +02:00
public static function update_teaching_cache($userid) {
global $DB;
$list = [];
2023-08-24 23:02:41 +02:00
// First find all active study plans.
2023-08-24 23:02:41 +02:00
$sql = "SELECT p.id FROM {local_treestudyplan_page} p
2023-08-25 17:33:20 +02:00
WHERE startdate <= NOW() and enddate >= NOW()";
2023-08-25 09:33:42 +02:00
$pageids = $DB->get_fieldset_sql($sql, []);
2023-08-25 09:44:34 +02:00
// Then parse them to see if the user has the grading permission in any of them .
2023-08-24 23:02:41 +02:00
// (Which would make them a teacher for all intents and purposes).
2023-08-25 09:33:42 +02:00
foreach ($pageids as $pageid) {
2023-08-24 23:02:41 +02:00
$sql = "SELECT i.course_id FROM {local_treestudyplan_item} i
INNER JOIN {local_treestudyplan_line} l ON i.line_id = l.id
2023-08-03 18:45:13 +02:00
WHERE l.page_id = :page_id AND i.course_id IS NOT NULL";
2023-08-25 09:33:42 +02:00
$courseids = $DB->get_fieldset_sql($sql, ["page_id" => $pageid]);
$linked = false;
2023-08-25 09:33:42 +02:00
foreach ($courseids as $cid) {
if (self::is_teaching_course($cid, $userid)) {
2023-08-24 23:02:41 +02:00
$linked = true;
break; // No need to search further.
}
}
2023-08-24 23:02:41 +02:00
if ($linked) {
2023-08-25 09:33:42 +02:00
$list[] = $pageid;
}
}
2023-08-24 23:02:41 +02:00
// Now, clear the database of all records for this user.
2023-08-25 10:41:56 +02:00
$DB->delete_records(self::TABLE, ["teacher_id" => $userid]);
2023-08-24 23:02:41 +02:00
// And add new records for the found studyplans.
$now = time();
2023-08-25 09:33:42 +02:00
foreach ($list as $pageid) {
2023-08-24 23:02:41 +02:00
// Retrieve the studyplan id from the page.
2023-08-25 09:33:42 +02:00
$planid = $DB->get_field("local_treestudyplan_page", "studyplan_id", ["id" => $pageid]);
2023-08-25 10:41:56 +02:00
$DB->insert_record(self::TABLE, ["teacher_id" => $userid, "studyplan_id" => $planid, "update_time" => $now]);
}
return $list;
}
2023-08-27 17:00:23 +02:00
/**
* List of recognized teacher id's
* @return intp[]
*/
2023-08-24 23:02:41 +02:00
public static function list_teacher_ids() {
global $DB;
return $DB->get_fieldset_sql("SELECT DISTINCT teacher_id FROM {".self::TABLE."}");
}
2023-08-27 17:00:23 +02:00
/**
* Get the last time of update for the specified teacher
* @param int $teacherid User id of teacher
* @return int Unix timestamp
*/
2023-08-25 09:33:42 +02:00
public static function get_update_time($teacherid): int {
global $DB;
2023-08-25 17:33:20 +02:00
$r = $DB->get_field_sql("SELECT MIN(update_time) FROM {".self::TABLE."} WHERE teacher_id=:teacher_id",
2023-08-25 09:33:42 +02:00
["teacher_id" => $teacherid]);
return (int)($r->update_time);
}
2023-08-25 11:52:05 +02:00
}