213 lines
		
	
	
		
			No EOL
		
	
	
		
			6.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			213 lines
		
	
	
		
			No EOL
		
	
	
		
			6.9 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/>.
 | 
						|
/**
 | 
						|
 * Helper functions for web services
 | 
						|
 * @package    tool_sptoolboxmgr
 | 
						|
 * @copyright  2023 P.M. Kuipers
 | 
						|
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
						|
 */
 | 
						|
 | 
						|
namespace tool_sptoolboxmgr;
 | 
						|
 | 
						|
defined('MOODLE_INTERNAL') || die();
 | 
						|
require_once($CFG->dirroot.'/user/profile/lib.php');
 | 
						|
use \local_treestudyplan\studyplan;
 | 
						|
use \ValueError;
 | 
						|
use \stdClass;
 | 
						|
 | 
						|
/**
 | 
						|
 * Class containing helper functions and abstraction for getting profile field information
 | 
						|
 */
 | 
						|
class profilehelper {
 | 
						|
    static $enrollable_userids = null;
 | 
						|
 | 
						|
    public static function get_enrol_values() {
 | 
						|
        $vals = [];
 | 
						|
        $text = get_config("tool_sptoolboxmgr","enrolvalue");
 | 
						|
        if(strlen(trim($text)) > 0) {
 | 
						|
            foreach ( explode("|",$text) as $v) {
 | 
						|
                $vals[] = trim($v);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $vals;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function get_userfield($id,$userid) {
 | 
						|
        global $DB;
 | 
						|
        $field = $DB->get_record("user_info_field",["id" => $id]);
 | 
						|
        if ($field) {
 | 
						|
            $userfield = profile_get_user_field($field->datatype,$id,$userid);
 | 
						|
            return $userfield;
 | 
						|
        } else {
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static function enrollable_user_ids() {
 | 
						|
        global $DB;
 | 
						|
        if (!isset($enrollable_userids)) {
 | 
						|
            $enrollablefield = get_config("tool_sptoolboxmgr","enrolfield");
 | 
						|
            $enrollablevalues = self::get_enrol_values();
 | 
						|
            [$insql, $inparams] = $DB->get_in_or_equal($enrollablevalues);
 | 
						|
            $inparams["fieldid"] = $enrollablefield;
 | 
						|
            $sqlwhere = "fieldid = :fieldid AND data {$insql}";
 | 
						|
            self::$enrollable_userids = $DB->get_fieldset_select("user_info_data","userid",$sqlwhere,$inparams);
 | 
						|
        }
 | 
						|
        return self::$enrollable_userids;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function previously_enrollable_user_ids() {
 | 
						|
        global $DB;
 | 
						|
        $enrollable_ids = self::enrollable_user_ids();
 | 
						|
        [$insql, $inparams] = $DB->get_in_or_equal($enrollable_ids);
 | 
						|
 | 
						|
        $sqlwhere = "enrolled = 1 AND userid NOT {$insql}";
 | 
						|
        $userids = $DB->get_fieldset_select("tool_sptoolbox","userid",$sqlwhere,$inparams);
 | 
						|
        return $userids;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function get_coach_userid($userid) {
 | 
						|
        global $DB;
 | 
						|
        $coachfieldid = get_config("tool_sptoolboxmgr","coachfield");
 | 
						|
 | 
						|
        $coachfield = self::get_userfield($coachfieldid,$userid);
 | 
						|
        if (! $coachfield) {
 | 
						|
            throw new ValueError("Coach field not set for this user");
 | 
						|
        }
 | 
						|
        $coachidentifier = trim($coachfield->data);
 | 
						|
 | 
						|
        // First search coach by username.
 | 
						|
        $coach = $DB->get_record("user",["userid" => $coachidentifier]);
 | 
						|
        if (! $coach) {
 | 
						|
            // Next search coach by email.
 | 
						|
            $coach = $DB->get_record("user",["email" => $coachidentifier]);
 | 
						|
 | 
						|
            if (! $coach) {
 | 
						|
                // Next search coach by idnumber.
 | 
						|
                $coach = $DB->get_record("user",["idnumber" => $coachidentifier]);
 | 
						|
            }
 | 
						|
    
 | 
						|
        }
 | 
						|
        if ($coach) {
 | 
						|
            return $coach->id;
 | 
						|
        } else {
 | 
						|
            return 0;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static function get_track_identifier($userid) {
 | 
						|
        $trackfieldid = get_config("tool_sptoolboxmgr","trackfield");
 | 
						|
 | 
						|
        $trackfield = self::get_userfield($trackfieldid,$userid);
 | 
						|
        if (! $trackfield) {
 | 
						|
            throw new ValueError("Track field not set for this user");
 | 
						|
        }
 | 
						|
        $value = trim($trackfield->data);
 | 
						|
        return $value;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function get_track_plan($track_identifier) : studyplan {
 | 
						|
        $mapping = get_config("tool_sptoolboxmgr","trackmapping");
 | 
						|
        if ($mapping == "shortname") {
 | 
						|
            $plans = studyplan::find_by_shortname($track_identifier);
 | 
						|
        } elseif ($mapping == "idnumber") {
 | 
						|
            $plans = studyplan::find_by_idnumber($track_identifier);
 | 
						|
        } elseif ($mapping == "name") {
 | 
						|
            $plans = studyplan::find_by_fullname($track_identifier);
 | 
						|
        }
 | 
						|
 | 
						|
        // Return the first match
 | 
						|
        if (count($plans) > 0) {
 | 
						|
            return $plans[0];
 | 
						|
        } else {
 | 
						|
            throw new ValueError("Toolbox not found by mapping '{$mapping}': '$track_identifier'");
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static function register_changes($userid,$toolbox = null) {
 | 
						|
        global $DB;
 | 
						|
        
 | 
						|
        $createnew = false;
 | 
						|
        $field = $DB->get_record("tool_sptoolboxmgr",["userid" => $userid]);
 | 
						|
        if (! $field) {
 | 
						|
            $createnew = true;
 | 
						|
            $field = new \stdClass;
 | 
						|
            $field->userid = $userid;
 | 
						|
        }
 | 
						|
 | 
						|
        $field->enrolled = in_array($userid,self::enrollable_user_ids())?1:0;
 | 
						|
        $field->track = self::get_track_identifier($userid);
 | 
						|
        $field->coach = self::get_coach_userid($userid);
 | 
						|
 | 
						|
        if (isset($toolbox)) {
 | 
						|
            $field->toolboxid = $toolbox->id();
 | 
						|
        }
 | 
						|
 | 
						|
        if ($createnew) {
 | 
						|
            $DB->insert_record("tool_sptoolboxmgr",$field);
 | 
						|
        } else {
 | 
						|
            $DB->update_record("tool_sptoolboxmgr",$field);
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    public static function check_changes($userid) {
 | 
						|
        global $DB;
 | 
						|
 | 
						|
        $o = new \stdClass;
 | 
						|
        $o->changed = false;
 | 
						|
        $o->validfields = true;
 | 
						|
        $o->enrolchanged = false;
 | 
						|
        $o->trackchanged = false;
 | 
						|
        $o->coachchanged = false;
 | 
						|
        $o->toolbox = null;
 | 
						|
 | 
						|
        $o->track = self::get_track_identifier($userid);
 | 
						|
        $o->coach = self::get_coach_userid($userid);
 | 
						|
        $o->enrolled = in_array($userid,self::enrollable_user_ids())?1:0;
 | 
						|
       
 | 
						|
 | 
						|
        $field = $DB->get_record("tool_sptoolboxmgr",["userid" => $userid]);
 | 
						|
        if ($field ) {
 | 
						|
            if ($field->enrolled != $o->enrolled) {
 | 
						|
                $o->enrolchanged = true;
 | 
						|
                $o->changed = true;
 | 
						|
            }
 | 
						|
            if ($field->track != $o->track) {
 | 
						|
                $o->trackchanged = true;
 | 
						|
                $o->changed = true;
 | 
						|
            }
 | 
						|
            if ($field->coach != $o->coach) {
 | 
						|
                $o->coachchanged = true;
 | 
						|
                $o->changed = true;
 | 
						|
            }
 | 
						|
            if ($field->toolboxid) {
 | 
						|
                $o->toolbox = studyplan::find_by_id(($field->toolboxid));
 | 
						|
            }
 | 
						|
 | 
						|
        } else {
 | 
						|
            $o->changed = true;
 | 
						|
            $o->enrolchanged = true;
 | 
						|
            $o->trackchanged = true;
 | 
						|
            $o->coachchanged = true;
 | 
						|
            
 | 
						|
        }
 | 
						|
 | 
						|
        return $o;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
} |