PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cohort/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 108 lines | 61 code | 21 blank | 26 comment | 10 complexity | a98806c1745a91a133e1430ea6429ea1 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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_cohort
  20. * @copyright 2010 Petr Skoda {@link http://skodak.org}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->dirroot . '/lib/formslib.php');
  25. class cohort_edit_form extends moodleform {
  26. /**
  27. * Define the cohort edit form
  28. */
  29. public function definition() {
  30. $mform = $this->_form;
  31. $editoroptions = $this->_customdata['editoroptions'];
  32. $cohort = $this->_customdata['data'];
  33. $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
  34. $mform->addRule('name', get_string('required'), 'required', null, 'client');
  35. $mform->setType('name', PARAM_TEXT);
  36. $options = $this->get_category_options($cohort->contextid);
  37. $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
  38. $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
  39. $mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.
  40. $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
  41. $mform->setType('description_editor', PARAM_RAW);
  42. $mform->addElement('hidden', 'id');
  43. $mform->setType('id', PARAM_INT);
  44. $this->add_action_buttons();
  45. $this->set_data($cohort);
  46. }
  47. public function validation($data, $files) {
  48. global $DB;
  49. $errors = parent::validation($data, $files);
  50. $idnumber = trim($data['idnumber']);
  51. if ($idnumber === '') {
  52. // Fine, empty is ok.
  53. } else if ($data['id']) {
  54. $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
  55. if ($current->idnumber !== $idnumber) {
  56. if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
  57. $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
  58. }
  59. }
  60. } else {
  61. if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
  62. $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
  63. }
  64. }
  65. return $errors;
  66. }
  67. protected function get_category_options($currentcontextid) {
  68. global $CFG;
  69. require_once($CFG->libdir. '/coursecatlib.php');
  70. $displaylist = coursecat::make_categories_list('moodle/cohort:manage');
  71. $options = array();
  72. $syscontext = context_system::instance();
  73. if (has_capability('moodle/cohort:manage', $syscontext)) {
  74. $options[$syscontext->id] = $syscontext->get_context_name();
  75. }
  76. foreach ($displaylist as $cid=>$name) {
  77. $context = context_coursecat::instance($cid);
  78. $options[$context->id] = $name;
  79. }
  80. // Always add current - this is not likely, but if the logic gets changed it might be a problem.
  81. if (!isset($options[$currentcontextid])) {
  82. $context = context::instance_by_id($currentcontextid, MUST_EXIST);
  83. $options[$context->id] = $syscontext->get_context_name();
  84. }
  85. return $options;
  86. }
  87. }