moodle_local_treestudyplan/classes/reportinvite_form.php

117 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 17:00:23 +02:00
* Form to edit invitations to view the user's studyplans
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
*/
2023-08-25 12:04:27 +02:00
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/formslib.php");
require_once("$CFG->dirroot/local/treestudyplan/lib.php");
2023-08-27 17:00:23 +02:00
/**
2023-08-27 22:20:17 +02:00
* Form to edit invitations to view the user's studyplans
2023-08-27 17:00:23 +02:00
*/
class reportinvite_form extends moodleform {
2023-08-25 12:16:51 +02:00
// Add elements to form.
2023-08-27 17:00:23 +02:00
/**
* Setup form
*/
public function definition() {
global $CFG;
2023-08-24 23:02:41 +02:00
2023-08-25 13:04:19 +02:00
$mform = $this->_form; // Don't forget the underscore!
2023-08-25 10:41:56 +02:00
$mform->addElement('hidden', 'add', 0);
$mform->setType('add', PARAM_ALPHANUM);
$mform->addElement('hidden', 'update', 0);
$mform->setType('update', PARAM_INT);
2023-08-25 11:52:05 +02:00
$mform->addElement('text', 'name', get_string('invite_name', 'local_treestudyplan'), array('size' => 50));
2023-08-25 12:16:51 +02:00
$mform->setType('name', PARAM_NOTAGS); // Set type of element.
$mform->setDefault('name', ''); // Default value.
2023-08-25 09:44:34 +02:00
$mform->addRule('name', get_string('required'), 'required', null, 'client');
2023-08-25 11:52:05 +02:00
$mform->addElement('text', 'email', get_string('invite_email', 'local_treestudyplan'), array('size' => 20));
2023-08-25 12:16:51 +02:00
$mform->setType('email', PARAM_NOTAGS); // Set type of element.
$mform->setDefault('email', ''); // Default value.
2023-08-25 09:44:34 +02:00
$mform->addRule('email', get_string('required'), 'required', null, 'client');
$mform->addRule('email', get_string('email'), 'email', null, 'client');
2023-08-25 11:52:05 +02:00
$mform->addElement('static', get_string('invite_email', 'local_treestudyplan') );
2023-08-25 09:44:34 +02:00
$this->add_action_buttons();
}
2023-08-27 22:20:17 +02:00
/**
2023-08-27 17:00:23 +02:00
* Custom validation should be added here.
* @param array $data Supplied user data
* @param array $files Any uploaded files
* @return array validation data
*/
2023-08-25 12:04:27 +02:00
public function validation($data, $files) {
return array();
}
2023-08-27 17:00:23 +02:00
/**
* Get supplied user data
2023-08-31 07:40:55 +02:00
* @return stdClass The supplied data
2023-08-27 17:00:23 +02:00
*/
2023-08-25 12:04:27 +02:00
public function get_data() {
2023-08-25 09:44:34 +02:00
global $DB, $USER;
$data = parent::get_data();
2023-08-25 12:16:51 +02:00
if ($data != null) {
2023-08-25 10:41:56 +02:00
if (empty($data->user_id)) {
2023-08-25 09:44:34 +02:00
$data->user_id = $USER->id;
}
2023-08-25 10:41:56 +02:00
if (empty($data->update)) {
2023-08-25 09:44:34 +02:00
$date = new DateTime("now", core_date::get_user_timezone_object());
2023-08-25 10:41:56 +02:00
$date->setTime(0, 0, 0);
2023-08-25 09:44:34 +02:00
$data->idate = $date->getTimeStamp();
}
2023-08-25 10:41:56 +02:00
if (empty($data->update)) {
2023-08-25 12:16:51 +02:00
// Create a new random key for the invite.
2023-08-25 09:44:34 +02:00
do {
$length = 20;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2023-08-25 13:04:19 +02:00
$characterslength = strlen($characters);
2023-08-25 09:44:34 +02:00
$randomkey = '';
for ($i = 0; $i < $length; $i++) {
2023-08-25 17:33:20 +02:00
$randomkey .= $characters[rand(0, $characterslength - 1)];
2023-08-25 09:44:34 +02:00
}
// Double check that the key is unique before inserting.
2023-08-25 13:04:19 +02:00
} while ($DB->record_exists_select("local_treestudyplan_invit",
2023-08-25 11:52:05 +02:00
$DB->sql_compare_text("invitekey"). " = " . $DB->sql_compare_text(":invitekey"),
['invitekey' => $randomkey]));
2023-08-25 09:44:34 +02:00
$data->invitekey = $randomkey;
}
}
return $data;
}
2023-08-25 11:52:05 +02:00
}