PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/enrol/instances.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 244 lines | 176 code | 36 blank | 32 comment | 46 complexity | b2dda04b6f2c0b8d15ba9e67bbc211f2 MD5 | raw file
  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. * Main course enrolment management UI.
  18. *
  19. * @package core
  20. * @subpackage enrol
  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. require('../config.php');
  25. $id = required_param('id', PARAM_INT); // course id
  26. $action = optional_param('action', '', PARAM_ALPHANUMEXT);
  27. $instanceid = optional_param('instance', 0, PARAM_INT);
  28. $confirm = optional_param('confirm', 0, PARAM_BOOL);
  29. $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
  30. $context = context_course::instance($course->id, MUST_EXIST);
  31. if ($course->id == SITEID) {
  32. redirect("$CFG->wwwroot/");
  33. }
  34. require_login($course);
  35. require_capability('moodle/course:enrolreview', $context);
  36. $canconfig = has_capability('moodle/course:enrolconfig', $context);
  37. $PAGE->set_url('/enrol/instances.php', array('id'=>$course->id));
  38. $PAGE->set_pagelayout('admin');
  39. $PAGE->set_title(get_string('enrolmentinstances', 'enrol'));
  40. $PAGE->set_heading($course->fullname);
  41. $instances = enrol_get_instances($course->id, false);
  42. $plugins = enrol_get_plugins(false);
  43. if ($canconfig and $action and confirm_sesskey()) {
  44. if (isset($instances[$instanceid]) and isset($plugins[$instances[$instanceid]->enrol])) {
  45. if ($action === 'up') {
  46. $order = array_keys($instances);
  47. $order = array_flip($order);
  48. $pos = $order[$instanceid];
  49. if ($pos > 0) {
  50. $switch = $pos - 1;
  51. $resorted = array_values($instances);
  52. $temp = $resorted[$pos];
  53. $resorted[$pos] = $resorted[$switch];
  54. $resorted[$switch] = $temp;
  55. // now update db sortorder
  56. foreach ($resorted as $sortorder=>$instance) {
  57. if ($instance->sortorder != $sortorder) {
  58. $instance->sortorder = $sortorder;
  59. $DB->update_record('enrol', $instance);
  60. }
  61. }
  62. }
  63. redirect($PAGE->url);
  64. } else if ($action === 'down') {
  65. $order = array_keys($instances);
  66. $order = array_flip($order);
  67. $pos = $order[$instanceid];
  68. if ($pos < count($instances) - 1) {
  69. $switch = $pos + 1;
  70. $resorted = array_values($instances);
  71. $temp = $resorted[$pos];
  72. $resorted[$pos] = $resorted[$switch];
  73. $resorted[$switch] = $temp;
  74. // now update db sortorder
  75. foreach ($resorted as $sortorder=>$instance) {
  76. if ($instance->sortorder != $sortorder) {
  77. $instance->sortorder = $sortorder;
  78. $DB->update_record('enrol', $instance);
  79. }
  80. }
  81. }
  82. redirect($PAGE->url);
  83. } else if ($action === 'delete') {
  84. $instance = $instances[$instanceid];
  85. $plugin = $plugins[$instance->enrol];
  86. if ($confirm) {
  87. $plugin->delete_instance($instance);
  88. redirect($PAGE->url);
  89. }
  90. echo $OUTPUT->header();
  91. $yesurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id, 'action'=>'delete', 'instance'=>$instance->id, 'confirm'=>1,'sesskey'=>sesskey()));
  92. $displayname = $plugin->get_instance_name($instance);
  93. $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
  94. if ($users) {
  95. $message = markdown_to_html(get_string('deleteinstanceconfirm', 'enrol', array('name'=>$displayname, 'users'=>$users)));
  96. } else {
  97. $message = markdown_to_html(get_string('deleteinstancenousersconfirm', 'enrol', array('name'=>$displayname)));
  98. }
  99. echo $OUTPUT->confirm($message, $yesurl, $PAGE->url);
  100. echo $OUTPUT->footer();
  101. die();
  102. } else if ($action === 'disable') {
  103. $instance = $instances[$instanceid];
  104. $plugin = $plugins[$instance->enrol];
  105. if ($instance->status != ENROL_INSTANCE_DISABLED) {
  106. $plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
  107. redirect($PAGE->url);
  108. }
  109. } else if ($action === 'enable') {
  110. $instance = $instances[$instanceid];
  111. $plugin = $plugins[$instance->enrol];
  112. if ($instance->status != ENROL_INSTANCE_ENABLED) {
  113. $plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
  114. redirect($PAGE->url);
  115. }
  116. }
  117. }
  118. }
  119. echo $OUTPUT->header();
  120. echo $OUTPUT->heading(get_string('enrolmentinstances', 'enrol'));
  121. echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
  122. // display strings
  123. $strup = get_string('up');
  124. $strdown = get_string('down');
  125. $strdelete = get_string('delete');
  126. $strenable = get_string('enable');
  127. $strdisable = get_string('disable');
  128. $strmanage = get_string('manageinstance', 'enrol');
  129. $table = new html_table();
  130. $table->head = array(get_string('name'), get_string('users'), $strup.'/'.$strdown, get_string('edit'));
  131. $table->align = array('left', 'center', 'center', 'center');
  132. $table->width = '100%';
  133. $table->data = array();
  134. // iterate through enrol plugins and add to the display table
  135. $updowncount = 1;
  136. $icount = count($instances);
  137. $url = new moodle_url('/enrol/instances.php', array('sesskey'=>sesskey(), 'id'=>$course->id));
  138. foreach ($instances as $instance) {
  139. if (!isset($plugins[$instance->enrol])) {
  140. continue;
  141. }
  142. $plugin = $plugins[$instance->enrol];
  143. $displayname = $plugin->get_instance_name($instance);
  144. if (!enrol_is_enabled($instance->enrol) or $instance->status != ENROL_INSTANCE_ENABLED) {
  145. $displayname = html_writer::tag('span', $displayname, array('class'=>'dimmed_text'));
  146. }
  147. $users = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id));
  148. $updown = array();
  149. $edit = array();
  150. if ($canconfig) {
  151. // up/down link
  152. $updown = '';
  153. if ($updowncount > 1) {
  154. $aurl = new moodle_url($url, array('action'=>'up', 'instance'=>$instance->id));
  155. $updown[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/up'), 'alt'=>$strup, 'class'=>'smallicon')));
  156. } else {
  157. $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'smallicon'));
  158. }
  159. if ($updowncount < $icount) {
  160. $aurl = new moodle_url($url, array('action'=>'down', 'instance'=>$instance->id));
  161. $updown[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/down'), 'alt'=>$strdown, 'class'=>'smallicon')));
  162. } else {
  163. $updown[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('spacer'), 'alt'=>'', 'class'=>'smallicon'));
  164. }
  165. ++$updowncount;
  166. // edit links
  167. if ($plugin->instance_deleteable($instance)) {
  168. $aurl = new moodle_url($url, array('action'=>'delete', 'instance'=>$instance->id));
  169. $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>$strdelete, 'class'=>'smallicon')));
  170. }
  171. if (enrol_is_enabled($instance->enrol)) {
  172. if ($instance->status == ENROL_INSTANCE_ENABLED) {
  173. $aurl = new moodle_url($url, array('action'=>'disable', 'instance'=>$instance->id));
  174. $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/hide'), 'alt'=>$strdisable, 'class'=>'smallicon')));
  175. } else if ($instance->status == ENROL_INSTANCE_DISABLED) {
  176. $aurl = new moodle_url($url, array('action'=>'enable', 'instance'=>$instance->id));
  177. $edit[] = html_writer::link($aurl, html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>$strenable, 'class'=>'smallicon')));
  178. } else {
  179. // plugin specific state - do not mess with it!
  180. $edit[] = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>'', 'class'=>'smallicon'));
  181. }
  182. }
  183. }
  184. // link to instance management
  185. if (enrol_is_enabled($instance->enrol)) {
  186. if ($icons = $plugin->get_action_icons($instance)) {
  187. $edit = array_merge($edit, $icons);
  188. }
  189. }
  190. // add a row to the table
  191. $table->data[] = array($displayname, $users, implode('&nbsp;', $updown), implode('&nbsp;', $edit));
  192. }
  193. echo html_writer::table($table);
  194. // access security is in each plugin
  195. $candidates = array();
  196. foreach (enrol_get_plugins(true) as $name=>$plugin) {
  197. if (!$link = $plugin->get_newinstance_link($course->id)) {
  198. continue;
  199. }
  200. $candidates[$link->out(false)] = get_string('pluginname', 'enrol_'.$name);
  201. }
  202. if ($candidates) {
  203. $select = new url_select($candidates);
  204. $select->set_label(get_string('addinstance', 'enrol'));
  205. echo $OUTPUT->render($select);
  206. }
  207. echo $OUTPUT->box_end();
  208. echo $OUTPUT->footer();