PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/cohort/edit_form.php

https://bitbucket.org/andrewdavidson/sl-clone
PHP | 112 lines | 63 code | 22 blank | 27 comment | 10 complexity | eb431dca461c6e4416cbc4d950d5019a MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Cohort related management functions, this file needs to be included manually.
  18. *
  19. * @package core
  20. * @subpackage cohort
  21. * @copyright 2010 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. if (!defined('MOODLE_INTERNAL')) {
  25. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  26. }
  27. require_once($CFG->dirroot . '/lib/formslib.php');
  28. class cohort_edit_form extends moodleform {
  29. /**
  30. * Define the cohort edit form
  31. */
  32. public function definition() {
  33. $mform = $this->_form;
  34. $editoroptions = $this->_customdata['editoroptions'];
  35. $cohort = $this->_customdata['data'];
  36. $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
  37. $mform->addRule('name', get_string('required'), 'required', null, 'client');
  38. $mform->setType('name', PARAM_MULTILANG);
  39. $options = $this->get_category_options($cohort->contextid);
  40. $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
  41. $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
  42. $mform->setType('idnumber', PARAM_RAW); // idnumbers are plain text, must not be changed
  43. $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
  44. $mform->setType('description_editor', PARAM_RAW);
  45. $mform->addElement('hidden', 'id');
  46. $mform->setType('id', PARAM_INT);
  47. $this->add_action_buttons();
  48. $this->set_data($cohort);
  49. }
  50. public function validation($data, $files) {
  51. global $DB;
  52. $errors = parent::validation($data, $files);
  53. $idnumber = trim($data['idnumber']);
  54. if ($idnumber === '') {
  55. // fine, empty is ok
  56. } else if ($data['id']) {
  57. $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
  58. if ($current->idnumber !== $idnumber) {
  59. if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
  60. $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
  61. }
  62. }
  63. } else {
  64. if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
  65. $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
  66. }
  67. }
  68. return $errors;
  69. }
  70. protected function get_category_options($currentcontextid) {
  71. $displaylist = array();
  72. $parentlist = array();
  73. make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
  74. $options = array();
  75. $syscontext = get_context_instance(CONTEXT_SYSTEM);
  76. if (has_capability('moodle/cohort:manage', $syscontext)) {
  77. $options[$syscontext->id] = print_context_name($syscontext);
  78. }
  79. foreach ($displaylist as $cid=>$name) {
  80. $context = get_context_instance(CONTEXT_COURSECAT, $cid, MUST_EXIST);
  81. $options[$context->id] = $name;
  82. }
  83. // always add current - this is not likely, but if the logic gets changed it might be a problem
  84. if (!isset($options[$currentcontextid])) {
  85. $context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
  86. $options[$context->id] = print_context_name($syscontext);
  87. }
  88. return $options;
  89. }
  90. }