-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmass_enroll.php
108 lines (77 loc) · 3.69 KB
/
mass_enroll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
// $Id: inscriptions_massives.php 356 2010-02-27 13:15:34Z ppollet $
/**
* A bulk enrolment plugin that allow teachers to massively enrol existing accounts to their courses,
* with an option of adding every user to a group
* Version for Moodle 1.9.x courtesy of Patrick POLLET & Valery FREMAUX France, February 2010
* Version for Moodle 2.x by [email protected] March 2012
*/
require(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once($CFG->libdir.'/csvlib.class.php');
require_once ('lib.php');
require_once ('mass_enroll_form.php');
/// Get params
$id = required_param('id', PARAM_INT);
if (!$course = $DB->get_record('course', array('id' => $id))) {
error("Course is misconfigured");
}
/// Security and access check
require_course_login($course);
$context = context_course::instance($course->id);
require_capability('local/mass_enroll:enrol', $context);
/// Start making page
$PAGE->set_pagelayout('incourse');
$PAGE->set_url('/local/mass_enroll/mass_enroll.php', array('id'=>$id));
$strinscriptions = get_string('mass_enroll', 'local_mass_enroll');
$PAGE->set_title($course->fullname . ': ' . $strinscriptions);
$PAGE->set_heading($course->fullname . ': ' . $strinscriptions);
echo $OUTPUT->header();
// Add tabs
$currenttab = 'mass_enroll';
require('tabs.php');
$mform = new mass_enroll_form($CFG->wwwroot . '/local/mass_enroll/mass_enroll.php', array (
'course' => $course,
'context' => $context
));
if ($mform->is_cancelled()) {
redirect(new moodle_url('/course/view.php', array('id'=>$id)));
} else
if ($data = $mform->get_data(false)) { // no magic quotes
echo $OUTPUT->heading($strinscriptions);
$iid = csv_import_reader::get_new_iid('uploaduser');
$cir = new csv_import_reader($iid, 'uploaduser');
$content = $mform->get_file_content('attachment');
$readcount = $cir->load_csv_content($content, $data->encoding, $data->delimiter_name);
unset($content);
if ($readcount === false) {
print_error('csvloaderror', '', $returnurl);
} else if ($readcount == 0) {
print_error('csvemptyfile', 'error', $returnurl);
}
$result = mass_enroll($cir, $course, $context, $data);
$cir->close();
$cir->cleanup(false); // only currently uploaded CSV file
if ($data->mailreport) {
$a = new StdClass();
$a->course = $course->fullname;
$a->report = $result;
email_to_user($USER, $USER, get_string('mail_enrolment_subject', 'local_mass_enroll', $CFG->wwwroot),
get_string('mail_enrolment', 'local_mass_enroll', $a));
$result .= "\n" . get_string('email_sent', 'local_mass_enroll', $USER->email);
}
echo $OUTPUT->box(nl2br($result), 'center');
echo $OUTPUT->continue_button($PAGE->url); // Back to this page
echo $OUTPUT->footer($course);
//path must be relative to 'module name', here 'course'
//add_to_log($course->id, 'course', 'enrol', '../local/mass_enroll/mass_enroll.php?id='.$course->id,$strinscriptions);
// Rev 12/11/2014 : some core function (get_recent_enrolments()) expect the info field of log record to be integer
// when action field is 'enrol'. This produced fatal SQL errors with PostGres see https://github.com/patrickpollet/moodle_local_mass_enroll/issues/5
// so we changed action value from 'enrol' to 'massenrol'
add_to_log($course->id, 'course', 'massenrol', '../local/mass_enroll/mass_enroll.php?id='.$course->id,$strinscriptions);
die();
}
echo $OUTPUT->heading_with_help($strinscriptions, 'mass_enroll', 'local_mass_enroll','icon',get_string('mass_enroll', 'local_mass_enroll'));
echo $OUTPUT->box (get_string('mass_enroll_info', 'local_mass_enroll'), 'center');
$mform->display();
echo $OUTPUT->footer($course);
?>