3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
27 * Allow the administrator to look through a list of course requests and approve or reject them.
29 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
33 require_once(dirname(__FILE__
) . '/../config.php');
34 require_once($CFG->libdir
. '/pagelib.php');
35 require_once($CFG->libdir
. '/blocklib.php');
36 require_once($CFG->libdir
. '/adminlib.php');
37 require_once($CFG->dirroot
. '/course/lib.php');
38 require_once($CFG->dirroot
. '/course/request_form.php');
41 require_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM
));
43 $approve = optional_param('approve', 0, PARAM_INT
);
44 $reject = optional_param('reject', 0, PARAM_INT
);
46 $baseurl = $CFG->wwwroot
. '/course/pending.php';
47 admin_externalpage_setup('coursespending');
49 /// Process approval of a course.
50 if (!empty($approve) and confirm_sesskey()) {
52 if (!$course = get_record('course_request', 'id', $approve)) {
53 print_error('unknowncourserequest');
55 foreach (array_keys((array)$course) as $key) {
56 $course->$key = addslashes($course->$key);
59 /// Ensure all is well before proceeding.
60 fix_course_sortorder();
61 if (empty($CFG->defaultrequestcategory
) or !record_exists('course_categories', 'id', $CFG->defaultrequestcategory
)) {
62 /// default to first top level directory, hacky but means things don't break
63 $CFG->defaultrequestcategory
= get_field('course_categories', 'id', 'parent', '0');
66 /// Build up a course record based on the request.
67 $course->category
= $CFG->defaultrequestcategory
;
68 $course->sortorder
= get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$course->category");
69 if (empty($course->sortorder
)) {
70 $course->sortorder
= 1000;
72 $course->requested
= 1;
73 unset($course->reason
);
75 $teacherid = $course->requester
;
76 unset($course->requester
);
77 $course->teacher
= get_string("defaultcourseteacher");
78 $course->teachers
= get_string("defaultcourseteachers");
79 $course->student
= get_string("defaultcoursestudent");
80 $course->students
= get_string("defaultcoursestudents");
81 if (!empty($CFG->restrictmodulesfor
) && $CFG->restrictmodulesfor
!= 'none' && !empty($CFG->restrictbydefault
)) {
82 $course->restrictmodules
= 1;
85 /// Apply course default settings
86 $courseconfig = get_config('moodlecourse');
87 $course->format
= $courseconfig->format
;
88 $course->numsections
= $courseconfig->numsections
;
89 $course->hiddensections
= $courseconfig->hiddensections
;
90 $course->newsitems
= $courseconfig->newsitems
;
91 $course->showgrades
= $courseconfig->showgrades
;
92 $course->showreports
= $courseconfig->showreports
;
93 $course->maxbytes
= $courseconfig->maxbytes
;
95 /// Insert the record.
96 if ($courseid = insert_record('course', $course)) {
97 $page = page_create_object(PAGE_COURSE_VIEW
, $courseid);
98 blocks_repopulate_page($page); // Return value not checked because you can always edit later
99 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
100 role_assign($CFG->creatornewroleid
, $teacherid, 0, $context->id
); // assing teacher role
101 $course->id
= $courseid;
102 if (!empty($CFG->restrictmodulesfor
) && $CFG->restrictmodulesfor
!= 'none' && !empty($CFG->restrictbydefault
)) { // if we're all or requested we're ok.
103 $allowedmods = explode(',',$CFG->defaultallowedmodules
);
104 update_restricted_mods($course,$allowedmods);
106 delete_records('course_request','id',$approve);
109 if (!empty($success)) {
110 $user = get_record('user','id',$teacherid);
111 $a->name
= $course->fullname
;
112 $a->url
= $CFG->wwwroot
.'/course/view.php?id='.$courseid;
113 $a->teacher
= $course->teacher
;
114 email_to_user($user, $USER, get_string('courseapprovedsubject'),
115 get_string('courseapprovedemail', 'moodle', $a));
116 redirect($CFG->wwwroot
.'/course/edit.php?id='.$courseid);
119 print_error('courseapprovedfailed');
123 /// Process rejection of a course.
124 if (!empty($reject)) {
125 /// Load the request.
126 if (!$course = get_record('course_request', 'id', $reject)) {
127 print_error('unknowncourserequest');
130 /// Prepare the form.
131 $rejectform = new reject_request_form($baseurl);
132 $default = new stdClass();
133 $default->reject
= $reject;
134 $rejectform->set_data($default);
136 /// Standard form processing if statement.
137 if ($rejectform->is_cancelled()){
140 } else if ($data = $rejectform->get_data()) {
141 /// Send an email to the requester.
142 $user = get_record('user', 'id', $course->requester
);
143 email_to_user($user, $USER, get_string('courserejectsubject'),
144 get_string('courserejectemail', '', $data->rejectnotice
));
146 /// Delete the request
147 delete_records('course_request', 'id', $course->id
);
149 /// Redirect back to the course listing.
150 redirect($baseurl, get_string('courserejected'));
153 /// Display the form for giving a reason for rejecting the request.
154 admin_externalpage_print_header($rejectform->focus());
155 $rejectform->display();
156 admin_externalpage_print_footer();
160 /// Print a list of all the pending requests.
161 admin_externalpage_print_header();
163 $pending = get_records('course_request');
164 if (empty($pending)) {
165 print_heading(get_string('nopendingcourses'));
167 print_heading(get_string('coursespending'));
169 /// Build a table of all the requests.
170 $table->class = 'pendingcourserequests generaltable';
171 $table->align
= array('center', 'center', 'center', 'center', 'center', 'center', 'center');
172 $table->head
= array(' ', get_string('shortname'), get_string('fullname'),
173 get_string('requestedby'), get_string('summary'), get_string('requestreason'), get_string('action'));
174 $strrequireskey = get_string('requireskey');
176 /// Loop over requested courses.
177 foreach ($pending as $course) {
178 $requester = get_record('user', 'id', $course->requester
);
181 /// Check here for shortname collisions and warn about them.
182 if (record_exists('course', 'shortname', $course->shortname
)) {
183 $course->shortname
.= ' [*]';
187 /// Show an enrolment key icon in the first column if applicable.
188 if (!empty($course->password
)) {
189 $row[] = '<img hspace="1" alt="'.$strrequireskey.'" class="icon" src="'.$CFG->pixpath
.'/i/key.gif" />';
194 /// Info in the other columns.
195 $row[] = format_string($course->shortname
);
196 $row[] = format_string($course->fullname
);
197 $row[] = fullname($requester);
198 $row[] = format_string($course->summary
);
199 $row[] = format_string($course->reason
);
200 $row[] = print_single_button($baseurl, array('approve' => $course->id
, 'sesskey' => sesskey()), get_string('approve'), 'get', '', true) .
201 print_single_button($baseurl, array('reject' => $course->id
), get_string('rejectdots'), 'get', '', true);
203 /// Add the row to the table.
204 $table->data
[] = $row;
207 /// Display the table.
210 /// Message about name collisions, if necessary.
211 if (!empty($collision)) {
212 print_string('shortnamecollisionwarning');
216 /// Finish off the page.
217 print_single_button($CFG->wwwroot
. '/course/index.php', array(), get_string('backtocourselisting'));
218 admin_externalpage_print_footer();