PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/cohort/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 128 lines | 83 code | 24 blank | 21 comment | 12 complexity | 4026951e29c0a31c16429ee4d898c657 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. * Adds instance form
  18. *
  19. * @package enrol_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->libdir/formslib.php");
  25. class enrol_cohort_edit_form extends moodleform {
  26. function definition() {
  27. global $CFG, $DB;
  28. $mform = $this->_form;
  29. list($instance, $plugin, $course) = $this->_customdata;
  30. $coursecontext = context_course::instance($course->id);
  31. $enrol = enrol_get_plugin('cohort');
  32. $groups = array(0 => get_string('none'));
  33. foreach (groups_get_all_groups($course->id) as $group) {
  34. $groups[$group->id] = format_string($group->name, true, array('context'=>$coursecontext));
  35. }
  36. $mform->addElement('header','general', get_string('pluginname', 'enrol_cohort'));
  37. $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
  38. $mform->setType('name', PARAM_TEXT);
  39. $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'),
  40. ENROL_INSTANCE_DISABLED => get_string('no'));
  41. $mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options);
  42. if ($instance->id) {
  43. if ($cohort = $DB->get_record('cohort', array('id'=>$instance->customint1))) {
  44. $cohorts = array($instance->customint1=>format_string($cohort->name, true, array('context'=>context::instance_by_id($cohort->contextid))));
  45. } else {
  46. $cohorts = array($instance->customint1=>get_string('error'));
  47. }
  48. $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
  49. $mform->setConstant('customint1', $instance->customint1);
  50. $mform->hardFreeze('customint1', $instance->customint1);
  51. } else {
  52. $cohorts = array('' => get_string('choosedots'));
  53. list($sqlparents, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids());
  54. $sql = "SELECT id, name, idnumber, contextid
  55. FROM {cohort}
  56. WHERE contextid $sqlparents
  57. ORDER BY name ASC, idnumber ASC";
  58. $rs = $DB->get_recordset_sql($sql, $params);
  59. foreach ($rs as $c) {
  60. $context = context::instance_by_id($c->contextid);
  61. if (!has_capability('moodle/cohort:view', $context)) {
  62. continue;
  63. }
  64. $cohorts[$c->id] = format_string($c->name);
  65. }
  66. $rs->close();
  67. $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
  68. $mform->addRule('customint1', get_string('required'), 'required', null, 'client');
  69. }
  70. $roles = get_assignable_roles($coursecontext);
  71. $roles[0] = get_string('none');
  72. $roles = array_reverse($roles, true); // Descending default sortorder.
  73. $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles);
  74. $mform->setDefault('roleid', $enrol->get_config('roleid'));
  75. if ($instance->id and !isset($roles[$instance->roleid])) {
  76. if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) {
  77. $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS, true);
  78. $roles[$instance->roleid] = role_get_name($role, $coursecontext);
  79. } else {
  80. $roles[$instance->roleid] = get_string('error');
  81. }
  82. }
  83. $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups);
  84. $mform->addElement('hidden', 'courseid', null);
  85. $mform->setType('courseid', PARAM_INT);
  86. $mform->addElement('hidden', 'id', null);
  87. $mform->setType('id', PARAM_INT);
  88. if ($instance->id) {
  89. $this->add_action_buttons(true);
  90. } else {
  91. $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
  92. }
  93. $this->set_data($instance);
  94. }
  95. function validation($data, $files) {
  96. global $DB;
  97. $errors = parent::validation($data, $files);
  98. $params = array('roleid'=>$data['roleid'], 'customint1'=>$data['customint1'], 'courseid'=>$data['courseid'], 'id'=>$data['id']);
  99. if ($DB->record_exists_select('enrol', "roleid = :roleid AND customint1 = :customint1 AND courseid = :courseid AND enrol = 'cohort' AND id <> :id", $params)) {
  100. $errors['roleid'] = get_string('instanceexists', 'enrol_cohort');
  101. }
  102. return $errors;
  103. }
  104. }