PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/flatfile/enrol.php

https://github.com/nicolasconnault/moodle2.0
PHP | 326 lines | 214 code | 75 blank | 37 comment | 48 complexity | 4f753f56865e30d15d5c6c4ad952d50f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. // The following flags are set in the configuration
  3. // $CFG->enrol_flatfilelocation: where is the file we are looking for?
  4. // $CFG->enrol_emailstudents: send email to students when they are enrolled in a course
  5. // $CFG->enrol_emailteachers: send email to teachers when they are enrolled in a course
  6. // $CFG->enrol_emailadmins: email the log from the cron job to the admin
  7. require_once($CFG->libdir.'/eventslib.php');
  8. class enrolment_plugin_flatfile {
  9. var $log;
  10. /// Override the base config_form() function
  11. function config_form($frm) {
  12. global $CFG, $DB;
  13. $vars = array('enrol_flatfilelocation', 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins');
  14. foreach ($vars as $var) {
  15. if (!isset($frm->$var)) {
  16. $frm->$var = '';
  17. }
  18. }
  19. $roles = $DB->get_records('role', null, '', 'id, name, shortname');
  20. $ffconfig = get_config('enrol_flatfile');
  21. $frm->enrol_flatfilemapping = array();
  22. foreach($roles as $id => $record) {
  23. $frm->enrol_flatfilemapping[$id] = array(
  24. $record->name,
  25. isset($ffconfig->{"map_{$record->shortname}"}) ? $ffconfig->{"map_{$record->shortname}"} : $record->shortname
  26. );
  27. }
  28. include ("$CFG->dirroot/enrol/flatfile/config.html");
  29. }
  30. /// Override the base process_config() function
  31. function process_config($config) {
  32. global $DB;
  33. if (!isset($config->enrol_flatfilelocation)) {
  34. $config->enrol_flatfilelocation = '';
  35. }
  36. set_config('enrol_flatfilelocation', $config->enrol_flatfilelocation);
  37. if (!isset($config->enrol_mailstudents)) {
  38. $config->enrol_mailstudents = '';
  39. }
  40. set_config('enrol_mailstudents', $config->enrol_mailstudents);
  41. if (!isset($config->enrol_mailteachers)) {
  42. $config->enrol_mailteachers = '';
  43. }
  44. set_config('enrol_mailteachers', $config->enrol_mailteachers);
  45. if (!isset($config->enrol_mailadmins)) {
  46. $config->enrol_mailadmins = '';
  47. }
  48. set_config('enrol_mailadmins', $config->enrol_mailadmins);
  49. foreach($DB->get_records('role', null, '', 'id, shortname') as $id => $role) {
  50. if (isset($config->{"enrol_flatfilemapping_{$id}"})) {
  51. set_config('map_'.$role->shortname, $config->{"enrol_flatfilemapping_{$id}"}, 'enrol_flatfile');
  52. } else {
  53. set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile');
  54. }
  55. }
  56. return true;
  57. }
  58. /// Override the get_access_icons() function
  59. function get_access_icons($course) {
  60. }
  61. /**
  62. * Override the base cron() function to read in a file
  63. *
  64. * Comma separated file assumed to have four or six fields per line:
  65. * operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
  66. * where:
  67. * operation = add | del
  68. * role = student | teacher | teacheredit
  69. * idnumber(user) = idnumber in the user table NB not id
  70. * idnumber(course) = idnumber in the course table NB not id
  71. * starttime = start time (in seconds since epoch) - optional
  72. * endtime = end time (in seconds since epoch) - optional
  73. */
  74. function cron() {
  75. global $CFG, $DB;
  76. if (empty($CFG->enrol_flatfilelocation)) {
  77. $filename = "$CFG->dataroot/1/enrolments.txt"; // Default location
  78. } else {
  79. $filename = $CFG->enrol_flatfilelocation;
  80. }
  81. if ( file_exists($filename) ) {
  82. $this->log = userdate(time()) . "\n";
  83. $this->log .= "Flatfile enrol cron found file: $filename\n\n";
  84. if (($fh = fopen($filename, "r")) != false) {
  85. list($roles, $rolemap) = $this->get_roles();
  86. $line = 0;
  87. while (!feof($fh)) {
  88. $line++;
  89. $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
  90. /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
  91. if (count($fields) != 4 and count($fields) !=6) {
  92. if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
  93. $this->log .= "$line: Line incorrectly formatted - ignoring\n";
  94. }
  95. continue;
  96. }
  97. $fields[0] = trim(strtolower($fields[0]));
  98. $fields[1] = trim(strtolower($fields[1]));
  99. $fields[2] = trim($fields[2]);
  100. $fields[3] = trim($fields[3]);
  101. $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3] ";
  102. if (!empty($fields[5])) {
  103. $fields[4] = (int)trim($fields[4]);
  104. $fields[5] = (int)trim($fields[5]);
  105. $this->log .= "$fields[4] $fields[5]";
  106. } else {
  107. $fields[4] = 0;
  108. $fields[5] = 0;
  109. }
  110. $this->log .= ":";
  111. /// check correct formatting of operation field
  112. if ($fields[0] != "add" and $fields[0] != "del") {
  113. $this->log .= "Unknown operation in field 1 - ignoring line\n";
  114. continue;
  115. }
  116. /// check correct formatting of role field
  117. if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
  118. $this->log .= "Unknown role in field2 - ignoring line\n";
  119. continue;
  120. }
  121. if (! $user = $DB->get_record("user", array("idnumber"=>$fields[2]))) {
  122. $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
  123. continue;
  124. }
  125. if (! $course = $DB->get_record("course", array("idnumber"=>$fields[3]))) {
  126. $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
  127. continue;
  128. }
  129. if ($fields[4] > $fields[5]) {
  130. $this->log .= "Start time was later than end time - ignoring line\n";
  131. continue;
  132. }
  133. unset($elog);
  134. // Either field[1] is a name that appears in the mapping,
  135. // or it's an actual short name. It has to be one or the
  136. // other, or we don't get to this point.
  137. $roleid = isset($rolemap[$fields[1]]) ? $roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
  138. // Create/resurrect a context object
  139. $context = get_context_instance(CONTEXT_COURSE, $course->id);
  140. if ($fields[0] == 'add') {
  141. role_assign($roleid, $user->id, null, $context->id, $fields[4], $fields[5], 0, 'flatfile');
  142. } else {
  143. role_unassign($roleid, $user->id, null, $context->id);
  144. }
  145. if ( empty($elog) and ($fields[0] == "add") ) {
  146. if ($fields[1] == "student") {
  147. if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*,ra.hidden', 'ra.sortorder ASC')) {
  148. foreach ($teachers as $u) {
  149. if (!$u->hidden || has_capability('moodle/role:viewhiddenassigns', $context)) {
  150. $teacher = $u;
  151. break;
  152. }
  153. }
  154. }
  155. if (!isset($teacher)) {
  156. $teacher = get_admin();
  157. }
  158. } else {
  159. $teacher = get_admin();
  160. }
  161. if (!empty($CFG->enrol_mailstudents)) {
  162. $a->coursename = "$course->fullname";
  163. $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
  164. $eventdata = new object();
  165. $eventdata->modulename = 'moodle';
  166. $eventdata->userfrom = $teacher;
  167. $eventdata->userto = $user;
  168. $eventdata->subject = get_string("enrolmentnew", '', $course->shortname);
  169. $eventdata->fullmessage = get_string('welcometocoursetext', '', $a);
  170. $eventdata->fullmessageformat = FORMAT_PLAIN;
  171. $eventdata->fullmessagehtml = '';
  172. $eventdata->smallmessage = '';
  173. events_trigger('message_send', $eventdata);
  174. }
  175. if (!empty($CFG->enrol_mailteachers) && $teachers) {
  176. foreach($teachers as $teacher) {
  177. if (!$u->hidden || has_capability('moodle/role:viewhiddenassigns', $context)) {
  178. $a->course = "$course->fullname";
  179. $a->user = fullname($user);
  180. $eventdata = new object();
  181. $eventdata->modulename = 'moodle';
  182. $eventdata->userfrom = $user;
  183. $eventdata->userto = $teacher;
  184. $eventdata->subject = get_string("enrolmentnew", '', $course->shortname);
  185. $eventdata->fullmessage = get_string('enrolmentnewuser', '', $a);
  186. $eventdata->fullmessageformat = FORMAT_PLAIN;
  187. $eventdata->fullmessagehtml = '';
  188. $eventdata->smallmessage = '';
  189. events_trigger('message_send', $eventdata);
  190. }
  191. }
  192. }
  193. }
  194. if (empty($elog)) {
  195. $elog = "OK\n";
  196. }
  197. $this->log .= $elog;
  198. } // end of while loop
  199. fclose($fh);
  200. } // end of if(file_open)
  201. if(! @unlink($filename)) {
  202. $eventdata = new object();
  203. $eventdata->modulename = 'moodle';
  204. $eventdata->userfrom = get_admin();
  205. $eventdata->userto = get_admin();
  206. $eventdata->subject = get_string("filelockedmailsubject", "enrol_flatfile");
  207. $eventdata->fullmessage = get_string("filelockedmail", "enrol_flatfile", $filename);
  208. $eventdata->fullmessageformat = FORMAT_PLAIN;
  209. $eventdata->fullmessagehtml = '';
  210. $eventdata->smallmessage = '';
  211. events_trigger('message_send', $eventdata);
  212. $this->log .= "Error unlinking file $filename\n";
  213. }
  214. if (!empty($CFG->enrol_mailadmins)) {
  215. $eventdata = new object();
  216. $eventdata->modulename = 'moodle';
  217. $eventdata->userfrom = get_admin();
  218. $eventdata->userto = get_admin();
  219. $eventdata->subject = "Flatfile Enrolment Log";
  220. $eventdata->fullmessage = $this->log;
  221. $eventdata->fullmessageformat = FORMAT_PLAIN;
  222. $eventdata->fullmessagehtml = '';
  223. $eventdata->smallmessage = '';
  224. events_trigger('message_send', $eventdata);
  225. }
  226. } // end of if(file_exists)
  227. } // end of function
  228. /**
  229. * Returns a pair of arrays. The first is the set of roleids, indexed by
  230. * their shortnames. The second is the set of shortnames that have
  231. * mappings, indexed by those mappings.
  232. *
  233. * @return array ($roles, $rolemap)
  234. */
  235. function get_roles() {
  236. global $DB;
  237. // Get a list of all the roles in the database, indexed by their short names.
  238. $roles = $DB->get_records('role', null, '', 'shortname, id');
  239. array_walk($roles, create_function('&$value', '$value = $value->id;'));
  240. // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'.
  241. $ffconfig = get_config('enrol_flatfile');
  242. $rolemap = array();
  243. foreach($ffconfig as $name => $value) {
  244. if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
  245. $rolemap[$value] = $key;
  246. }
  247. }
  248. return array($roles, $rolemap);
  249. }
  250. } // end of class
  251. ?>