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

/user/profile/index_category_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 60 lines | 37 code | 18 blank | 5 comment | 7 complexity | abfcbe87e6c283b07ddeeaf77440e227 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. if (!defined('MOODLE_INTERNAL')) {
  3. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  4. }
  5. require_once($CFG->dirroot.'/lib/formslib.php');
  6. class category_form extends moodleform {
  7. // Define the form
  8. function definition () {
  9. global $USER, $CFG;
  10. $mform =& $this->_form;
  11. $strrequired = get_string('required');
  12. /// Add some extra hidden fields
  13. $mform->addElement('hidden', 'id');
  14. $mform->setType('id', PARAM_INT);
  15. $mform->addElement('hidden', 'action', 'editcategory');
  16. $mform->setType('action', PARAM_ALPHANUMEXT);
  17. $mform->addElement('text', 'name', get_string('profilecategoryname', 'admin'), 'maxlength="255" size="30"');
  18. $mform->setType('name', PARAM_TEXT);
  19. $mform->addRule('name', $strrequired, 'required', null, 'client');
  20. $this->add_action_buttons(true);
  21. } /// End of function
  22. /// perform some moodle validation
  23. function validation($data, $files) {
  24. global $CFG, $DB;
  25. $errors = parent::validation($data, $files);
  26. $data = (object)$data;
  27. $duplicate = $DB->get_field('user_info_category', 'id', array('name' => $data->name));
  28. /// Check the name is unique
  29. if (!empty($data->id)) { // we are editing an existing record
  30. $olddata = $DB->get_record('user_info_category', array('id'=>$data->id));
  31. // name has changed, new name in use, new name in use by another record
  32. $dupfound = (($olddata->name !== $data->name) && $duplicate && ($data->id != $duplicate));
  33. }
  34. else { // new profile category
  35. $dupfound = $duplicate;
  36. }
  37. if ($dupfound ) {
  38. $errors['name'] = get_string('profilecategorynamenotunique', 'admin');
  39. }
  40. return $errors;
  41. }
  42. }