first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+172
View File
@@ -0,0 +1,172 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.
namespace mod_quiz\form;
use core\check\performance\debugging;
use core_tag_tag;
use moodleform;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* The add random questions form.
*
* @package mod_quiz
* @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated Moodle 4.3 MDL-72321. This form is new generated in a modal with mod_quiz/add_random_question_form.mustache
* @todo Final deprecation in Moodle 4.7 MDL-78091
*/
class add_random_form extends moodleform {
/**
* Deprecated.
*
* @return void
* @deprecated Moodle 4.3 MDL-72321
* @todo Final deprecation in Moodle 4.7 MDL-78091
*/
protected function definition() {
debugging(
'add_random_form is deprecated. Please use mod_quiz/add_random_question_form.mustache instead.',
DEBUG_DEVELOPER
);
global $OUTPUT, $PAGE, $CFG;
$mform = $this->_form;
$mform->setDisableShortforms();
$contexts = $this->_customdata['contexts'];
$usablecontexts = $contexts->having_cap('moodle/question:useall');
// Random from existing category section.
$mform->addElement('header', 'existingcategoryheader',
get_string('randomfromexistingcategory', 'quiz'));
$mform->addElement('questioncategory', 'category', get_string('category'),
['contexts' => $usablecontexts, 'top' => true]);
$mform->setDefault('category', $this->_customdata['cat']);
$mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));
$tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id'));
$mform->hideIf('includesubcategories', 'category', 'in', $tops);
if ($CFG->usetags) {
$tagstrings = [];
$tags = core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts);
foreach ($tags as $tag) {
$tagstrings["{$tag->id},{$tag->name}"] = $tag->name;
}
$options = [
'multiple' => true,
'noselectionstring' => get_string('anytags', 'quiz'),
];
$mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options);
$mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz');
}
// TODO: in the past, the drop-down used to only show sensible choices for
// number of questions to add. That is, if the currently selected filter
// only matched 9 questions (not already in the quiz), then the drop-down would
// only offer choices 1..9. This nice UI hint got lost when the UI became Ajax-y.
// We should add it back.
$mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'),
$this->get_number_of_questions_to_add_choices());
$previewhtml = $OUTPUT->render_from_template('mod_quiz/random_question_form_preview', []);
$mform->addElement('html', $previewhtml);
$mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));
// If the manage categories plugins is enabled, add the elements to create a new category in the form.
if (\core\plugininfo\qbank::is_plugin_enabled(\qbank_managecategories\helper::PLUGINNAME)) {
// Random from a new category section.
$mform->addElement('header', 'newcategoryheader',
get_string('randomquestionusinganewcategory', 'quiz'));
$mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
$mform->setType('name', PARAM_TEXT);
$mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
['contexts' => $usablecontexts, 'top' => true]);
$mform->addHelpButton('parent', 'parentcategory', 'question');
$mform->addElement('submit', 'newcategory',
get_string('createcategoryandaddrandomquestion', 'quiz'));
}
// Cancel button.
$mform->addElement('cancel');
$mform->closeHeaderBefore('cancel');
$mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
$mform->setType('addonpage', PARAM_SEQUENCE);
$mform->addElement('hidden', 'cmid', 0);
$mform->setType('cmid', PARAM_INT);
$mform->addElement('hidden', 'returnurl', 0);
$mform->setType('returnurl', PARAM_LOCALURL);
// Add the javascript required to enhance this mform.
$PAGE->requires->js_call_amd('mod_quiz/add_random_form', 'init', [
$mform->getAttribute('id'),
$contexts->lowest()->id,
$tops,
$CFG->usetags
]);
}
/**
* Deprecated.
*
* @param array $fromform
* @param array $files
* @return array
* @deprecated Moodle 4.3 MDL-72321
* @todo Final deprecation in Moodle 4.7 MDL-78091
*/
public function validation($fromform, $files) {
debugging(
'add_random_form is deprecated. Please use mod_quiz/add_random_question_form.mustache instead.',
DEBUG_DEVELOPER
);
$errors = parent::validation($fromform, $files);
if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
$errors['name'] = get_string('categorynamecantbeblank', 'question');
}
return $errors;
}
/**
* Return an arbitrary array for the dropdown menu
*
* @param int $maxrand
* @return array of integers [1, 2, ..., 100] (or to the smaller of $maxrand and 100.)
*/
private function get_number_of_questions_to_add_choices($maxrand = 100) {
$randomcount = [];
for ($i = 1; $i <= min(100, $maxrand); $i++) {
$randomcount[$i] = $i;
}
return $randomcount;
}
}
@@ -0,0 +1,294 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.
namespace mod_quiz\form;
use cm_info;
use context;
use context_module;
use mod_quiz_mod_form;
use moodle_url;
use moodleform;
use stdClass;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->dirroot . '/mod/quiz/mod_form.php');
/**
* Form for editing quiz settings overrides.
*
* @package mod_quiz
* @copyright 2010 Matt Petro
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class edit_override_form extends moodleform {
/** @var cm_info course module object. */
protected $cm;
/** @var stdClass the quiz settings object. */
protected $quiz;
/** @var context_module the quiz context. */
protected $context;
/** @var bool editing group override (true) or user override (false). */
protected $groupmode;
/** @var int groupid, if provided. */
protected $groupid;
/** @var int userid, if provided. */
protected $userid;
/** @var int overrideid, if provided. */
protected int $overrideid;
/**
* Constructor.
*
* @param moodle_url $submiturl the form action URL.
* @param cm_info $cm course module object.
* @param stdClass $quiz the quiz settings object.
* @param context_module $context the quiz context.
* @param bool $groupmode editing group override (true) or user override (false).
* @param stdClass|null $override the override being edited, if it already exists.
*/
public function __construct(moodle_url $submiturl,
cm_info $cm, stdClass $quiz, context_module $context,
bool $groupmode, ?stdClass $override) {
$this->cm = $cm;
$this->quiz = $quiz;
$this->context = $context;
$this->groupmode = $groupmode;
$this->groupid = empty($override->groupid) ? 0 : $override->groupid;
$this->userid = empty($override->userid) ? 0 : $override->userid;
$this->overrideid = $override->id ?? 0;
parent::__construct($submiturl);
}
protected function definition() {
global $DB;
$cm = $this->cm;
$mform = $this->_form;
$mform->addElement('header', 'override', get_string('override', 'quiz'));
$quizgroupmode = groups_get_activity_groupmode($cm);
$accessallgroups = ($quizgroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $this->context);
if ($this->groupmode) {
// Group override.
if ($this->groupid) {
// There is already a groupid, so freeze the selector.
$groupchoices = [
$this->groupid => format_string(groups_get_group_name($this->groupid), true, ['context' => $this->context]),
];
$mform->addElement('select', 'groupid',
get_string('overridegroup', 'quiz'), $groupchoices);
$mform->freeze('groupid');
} else {
// Prepare the list of groups.
// Only include the groups the current can access.
$groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);
if (empty($groups)) {
// Generate an error.
$link = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $cm->id]);
throw new \moodle_exception('groupsnone', 'quiz', $link);
}
$groupchoices = [];
foreach ($groups as $group) {
if ($group->visibility != GROUPS_VISIBILITY_NONE) {
$groupchoices[$group->id] = format_string($group->name, true, ['context' => $this->context]);
}
}
unset($groups);
if (count($groupchoices) == 0) {
$groupchoices[0] = get_string('none');
}
$mform->addElement('select', 'groupid',
get_string('overridegroup', 'quiz'), $groupchoices);
$mform->addRule('groupid', get_string('required'), 'required', null, 'client');
}
} else {
// User override.
$userfieldsapi = \core_user\fields::for_identity($this->context)->with_userpic()->with_name();
$extrauserfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
if ($this->userid) {
// There is already a userid, so freeze the selector.
$user = $DB->get_record('user', ['id' => $this->userid]);
profile_load_custom_fields($user);
$userchoices = [];
$userchoices[$this->userid] = self::display_user_name($user, $extrauserfields);
$mform->addElement('select', 'userid',
get_string('overrideuser', 'quiz'), $userchoices);
$mform->freeze('userid');
} else {
// Prepare the list of users.
$groupids = 0;
if (!$accessallgroups) {
$groups = groups_get_activity_allowed_groups($cm);
$groupids = array_keys($groups);
}
$enrolledjoin = get_enrolled_with_capabilities_join(
$this->context, '', 'mod/quiz:attempt', $groupids, true);
$userfieldsql = $userfieldsapi->get_sql('u', true, '', '', false);
list($sort, $sortparams) = users_order_by_sql('u', null,
$this->context, $userfieldsql->mappings);
$users = $DB->get_records_sql("
SELECT DISTINCT $userfieldsql->selects
FROM {user} u
$enrolledjoin->joins
$userfieldsql->joins
LEFT JOIN {quiz_overrides} existingoverride ON
existingoverride.userid = u.id AND existingoverride.quiz = :quizid
WHERE existingoverride.id IS NULL
AND $enrolledjoin->wheres
ORDER BY $sort
", array_merge(['quizid' => $this->quiz->id], $userfieldsql->params, $enrolledjoin->params, $sortparams));
// Filter users based on any fixed restrictions (groups, profile).
$info = new \core_availability\info_module($cm);
$users = $info->filter_user_list($users);
if (empty($users)) {
// Generate an error.
$link = new moodle_url('/mod/quiz/overrides.php', ['cmid' => $cm->id]);
throw new \moodle_exception('usersnone', 'quiz', $link);
}
$userchoices = [];
foreach ($users as $id => $user) {
$userchoices[$id] = self::display_user_name($user, $extrauserfields);
}
unset($users);
$mform->addElement('searchableselector', 'userid',
get_string('overrideuser', 'quiz'), $userchoices);
$mform->addRule('userid', get_string('required'), 'required', null, 'client');
}
}
// Password.
// This field has to be above the date and timelimit fields,
// otherwise browsers will clear it when those fields are changed.
$mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
$mform->setType('password', PARAM_TEXT);
$mform->addHelpButton('password', 'requirepassword', 'quiz');
$mform->setDefault('password', $this->quiz->password);
// Open and close dates.
$mform->addElement('date_time_selector', 'timeopen',
get_string('quizopen', 'quiz'), mod_quiz_mod_form::$datefieldoptions);
$mform->setDefault('timeopen', $this->quiz->timeopen);
$mform->addElement('date_time_selector', 'timeclose',
get_string('quizclose', 'quiz'), mod_quiz_mod_form::$datefieldoptions);
$mform->setDefault('timeclose', $this->quiz->timeclose);
// Time limit.
$mform->addElement('duration', 'timelimit',
get_string('timelimit', 'quiz'), ['optional' => true]);
$mform->addHelpButton('timelimit', 'timelimit', 'quiz');
$mform->setDefault('timelimit', $this->quiz->timelimit);
// Number of attempts.
$attemptoptions = ['0' => get_string('unlimited')];
for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
$attemptoptions[$i] = $i;
}
$mform->addElement('select', 'attempts',
get_string('attemptsallowed', 'quiz'), $attemptoptions);
$mform->addHelpButton('attempts', 'attempts', 'quiz');
$mform->setDefault('attempts', $this->quiz->attempts);
// Submit buttons.
$mform->addElement('submit', 'resetbutton',
get_string('reverttodefaults', 'quiz'));
$buttonarray = [];
$buttonarray[] = $mform->createElement('submit', 'submitbutton',
get_string('save', 'quiz'));
$buttonarray[] = $mform->createElement('submit', 'againbutton',
get_string('saveoverrideandstay', 'quiz'));
$buttonarray[] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonbar', '', [' '], false);
$mform->closeHeaderBefore('buttonbar');
}
/**
* Get a user's name and identity ready to display.
*
* @param stdClass $user a user object.
* @param array $extrauserfields (identity fields in user table only from the user_fields API)
* @return string User's name, with extra info, for display.
*/
public static function display_user_name(stdClass $user, array $extrauserfields): string {
$username = fullname($user);
$namefields = [];
foreach ($extrauserfields as $field) {
if (isset($user->$field) && $user->$field !== '') {
$namefields[] = s($user->$field);
} else if (strpos($field, 'profile_field_') === 0) {
$field = substr($field, 14);
if (isset($user->profile[$field]) && $user->profile[$field] !== '') {
$namefields[] = s($user->profile[$field]);
}
}
}
if ($namefields) {
$username .= ' (' . implode(', ', $namefields) . ')';
}
return $username;
}
/**
* Validate the data from the form.
*
* @param array $data form data
* @param array $files form files
* @return array An array of error messages, where the key is is the mform element name and the value is the error.
*/
public function validation($data, $files): array {
$errors = parent::validation($data, $files);
$data['id'] = $this->overrideid;
$data['quiz'] = $this->quiz->id;
$manager = new \mod_quiz\local\override_manager($this->quiz, $this->context);
$errors = array_merge($errors, $manager->validate_data($data));
// Any 'general' errors we merge with the group/user selector element.
if (!empty($errors['general'])) {
if ($this->groupmode) {
$errors['groupid'] = $errors['groupid'] ?? "" . $errors['general'];
} else {
$errors['userid'] = $errors['userid'] ?? "" . $errors['general'];
}
}
return $errors;
}
}
@@ -0,0 +1,64 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.
namespace mod_quiz\form;
use moodleform;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* A form that limits student's access to attempt a quiz.
*
* @package mod_quiz
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class preflight_check_form extends moodleform {
protected function definition() {
$mform = $this->_form;
$this->_form->updateAttributes(['id' => 'mod_quiz_preflight_form']);
foreach ($this->_customdata['hidden'] as $name => $value) {
if ($name === 'sesskey') {
continue;
}
$mform->addElement('hidden', $name, $value);
$mform->setType($name, PARAM_INT);
}
foreach ($this->_customdata['rules'] as $rule) {
if ($rule->is_preflight_check_required($this->_customdata['attemptid'])) {
$rule->add_preflight_check_form_fields($this, $mform,
$this->_customdata['attemptid']);
}
}
$this->add_action_buttons(true, get_string('startattempt', 'quiz'));
$this->set_display_vertical();
$mform->setDisableShortforms();
}
public function validation($data, $files): array {
$errors = parent::validation($data, $files);
$accessmanager = $this->_customdata['quizobj']->get_access_manager(time());
return array_merge($errors, $accessmanager->validate_preflight_check(
$data, $files, $this->_customdata['attemptid']));
}
}
@@ -0,0 +1,100 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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 <http://www.gnu.org/licenses/>.
/**
* Defines the editing form for random questions.
*
* @package mod_quiz
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_quiz\form;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* Class randomquestion_form
*
* @package mod_quiz
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class randomquestion_form extends \moodleform {
/**
* Form definiton.
*/
public function definition() {
$mform = $this->_form;
$contexts = $this->_customdata['contexts'];
$usablecontexts = $contexts->having_cap('moodle/question:useall');
// Standard fields at the start of the form.
$mform->addElement('header', 'generalheader', get_string("general", 'form'));
$mform->addElement('questioncategory', 'category', get_string('category', 'question'),
['contexts' => $usablecontexts, 'top' => true]);
$mform->addElement('advcheckbox', 'includesubcategories', get_string('recurse', 'quiz'), null, null, [0, 1]);
$tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id'));
$mform->hideIf('includesubcategories', 'category', 'in', $tops);
$tags = \core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts);
$tagstrings = [];
foreach ($tags as $tag) {
$tagstrings["{$tag->id},{$tag->name}"] = $tag->name;
}
$options = [
'multiple' => true,
'noselectionstring' => get_string('anytags', 'quiz'),
];
$mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options);
$mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz');
$mform->addElement('hidden', 'slotid');
$mform->setType('slotid', PARAM_INT);
$mform->addElement('hidden', 'returnurl');
$mform->setType('returnurl', PARAM_LOCALURL);
$buttonarray = [];
$buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
$buttonarray[] = $mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
$mform->closeHeaderBefore('buttonar');
}
public function set_data($defaultvalues) {
$mform = $this->_form;
if ($defaultvalues->fromtags) {
$fromtagselement = $mform->getElement('fromtags');
foreach ($defaultvalues->fromtags as $fromtag) {
if (!$fromtagselement->optionExists($fromtag)) {
$optionname = get_string('randomfromunavailabletag', 'mod_quiz', explode(',', $fromtag)[1]);
$fromtagselement->addOption($optionname, $fromtag);
}
}
}
parent::set_data($defaultvalues);
}
}