PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/flatfile/enrol.php

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