|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00034 class enrol_cohort_plugin extends enrol_plugin { 00041 public function get_instance_name($instance) { 00042 global $DB; 00043 00044 if (empty($instance)) { 00045 $enrol = $this->get_name(); 00046 return get_string('pluginname', 'enrol_'.$enrol); 00047 } else if (empty($instance->name)) { 00048 $enrol = $this->get_name(); 00049 if ($role = $DB->get_record('role', array('id'=>$instance->roleid))) { 00050 $role = role_get_name($role, get_context_instance(CONTEXT_COURSE, $instance->courseid)); 00051 } else { 00052 $role = get_string('error'); 00053 } 00054 00055 return get_string('pluginname', 'enrol_'.$enrol) . ' (' . format_string($DB->get_field('cohort', 'name', array('id'=>$instance->customint1))) . ' - ' . $role .')'; 00056 } else { 00057 return format_string($instance->name); 00058 } 00059 } 00060 00066 public function get_newinstance_link($courseid) { 00067 if (!$this->can_add_new_instances($courseid)) { 00068 return NULL; 00069 } 00070 // multiple instances supported - multiple parent courses linked 00071 return new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$courseid)); 00072 } 00073 00081 protected function can_add_new_instances($courseid) { 00082 global $DB; 00083 00084 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid); 00085 if (!has_capability('moodle/course:enrolconfig', $coursecontext) or !has_capability('enrol/cohort:config', $coursecontext)) { 00086 return false; 00087 } 00088 list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext)); 00089 $sql = "SELECT id, contextid 00090 FROM {cohort} 00091 WHERE contextid $sqlparents 00092 ORDER BY name ASC"; 00093 $cohorts = $DB->get_records_sql($sql, $params); 00094 foreach ($cohorts as $c) { 00095 $context = get_context_instance_by_id($c->contextid); 00096 if (has_capability('moodle/cohort:view', $context)) { 00097 return true; 00098 } 00099 } 00100 return false; 00101 } 00102 00107 public function cron() { 00108 global $CFG; 00109 00110 // purge all roles if cohort sync disabled, those can be recreated later here in cron 00111 if (!enrol_is_enabled('cohort')) { 00112 role_unassign_all(array('component'=>'cohort_enrol')); 00113 return; 00114 } 00115 00116 require_once("$CFG->dirroot/enrol/cohort/locallib.php"); 00117 enrol_cohort_sync(); 00118 } 00119 00128 public function course_updated($inserted, $course, $data) { 00129 global $CFG; 00130 00131 if (!$inserted) { 00132 // sync cohort enrols 00133 require_once("$CFG->dirroot/enrol/cohort/locallib.php"); 00134 enrol_cohort_sync($course->id); 00135 } else { 00136 // cohorts are never inserted automatically 00137 } 00138 00139 } 00140 00150 public function get_manual_enrol_button(course_enrolment_manager $manager) { 00151 $course = $manager->get_course(); 00152 if (!$this->can_add_new_instances($course->id)) { 00153 return false; 00154 } 00155 00156 $cohorturl = new moodle_url('/enrol/cohort/addinstance.php', array('id' => $course->id)); 00157 $button = new enrol_user_button($cohorturl, get_string('enrolcohort', 'enrol'), 'get'); 00158 $button->class .= ' enrol_cohort_plugin'; 00159 00160 $button->strings_for_js(array( 00161 'enrol', 00162 'synced', 00163 'enrolcohort', 00164 'enrolcohortusers', 00165 ), 'enrol'); 00166 $button->strings_for_js(array( 00167 'ajaxmore', 00168 'cohortsearch', 00169 ), 'enrol_cohort'); 00170 $button->strings_for_js('assignroles', 'role'); 00171 $button->strings_for_js('cohort', 'cohort'); 00172 $button->strings_for_js('users', 'moodle'); 00173 00174 // No point showing this at all if the user cant manually enrol users 00175 $hasmanualinstance = has_capability('enrol/manual:enrol', $manager->get_context()) && $manager->has_instance('manual'); 00176 00177 $modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin'); 00178 $function = 'M.enrol_cohort.quickenrolment.init'; 00179 $arguments = array( 00180 'courseid' => $course->id, 00181 'ajaxurl' => '/enrol/cohort/ajax.php', 00182 'url' => $manager->get_moodlepage()->url->out(false), 00183 'manualEnrolment' => $hasmanualinstance); 00184 $button->require_yui_module($modules, $function, array($arguments)); 00185 00186 return $button; 00187 } 00188 } 00189 00190