2 // allow the administrators to look through a list of course requests and either approve them or reject them.
4 require_once('../config.php');
5 require_once($CFG->libdir
.'/pagelib.php');
6 require_once($CFG->libdir
.'/blocklib.php');
7 require_once('lib.php');
11 require_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM
));
13 $approve = optional_param('approve', 0, PARAM_INT
);
14 $reject = optional_param('reject', 0, PARAM_INT
);
15 $rejectnotice = optional_param('rejectnotice', '', PARAM_CLEANHTML
);
17 if (!empty($approve) and confirm_sesskey()) {
18 if ($course = get_record("course_request","id",$approve)) {
19 foreach (array_keys((array)$course) as $key) {
20 $course->$key = addslashes($course->$key);
23 // place at beginning of category
24 fix_course_sortorder();
26 if (empty($CFG->defaultrequestcategory
) or !record_exists('course_categories', 'id', $CFG->defaultrequestcategory
)) {
27 // default to first top level directory, hacky but means things don't break
28 $CFG->defaultrequestcategory
= get_field('course_categories', 'id', 'parent', '0');
31 $course->category
= $CFG->defaultrequestcategory
;
32 $course->sortorder
= get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$course->category");
33 if (empty($course->sortorder
)) {
34 $course->sortorder
= 1000;
36 $course->requested
= 1;
37 unset($course->reason
);
39 $teacherid = $course->requester
;
40 unset($course->requester
);
41 $course->teacher
= get_string("defaultcourseteacher");
42 $course->teachers
= get_string("defaultcourseteachers");
43 $course->student
= get_string("defaultcoursestudent");
44 $course->students
= get_string("defaultcoursestudents");
45 if (!empty($CFG->restrictmodulesfor
) && $CFG->restrictmodulesfor
!= 'none' && !empty($CFG->restrictbydefault
)) {
46 $course->restrictmodules
= 1;
48 if ($courseid = insert_record("course",$course)) {
49 $page = page_create_object(PAGE_COURSE_VIEW
, $courseid);
50 blocks_repopulate_page($page); // Return value not checked because you can always edit later
51 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
52 role_assign($CFG->creatornewroleid
, $teacherid, 0, $context->id
); // assing teacher role
53 $course->id
= $courseid;
54 if (!empty($CFG->restrictmodulesfor
) && $CFG->restrictmodulesfor
!= 'none' && !empty($CFG->restrictbydefault
)) { // if we're all or requested we're ok.
55 $allowedmods = explode(',',$CFG->defaultallowedmodules
);
56 update_restricted_mods($course,$allowedmods);
58 delete_records('course_request','id',$approve);
61 if (!empty($success)) {
62 $user = get_record('user','id',$teacherid);
63 $a->name
= $course->fullname
;
64 $a->url
= $CFG->wwwroot
.'/course/view.php?id='.$courseid;
65 $a->teacher
= $course->teacher
;
66 email_to_user($user,$USER,get_string('courseapprovedsubject'),get_string('courseapprovedemail','moodle',$a));
67 redirect($CFG->wwwroot
.'/course/edit.php?id='.$courseid);
71 print_error('courseapprovedfailed');
77 $strtitle = get_string('coursespending');
78 $strheading = get_string(((!empty($reject)) ?
'coursereject' : 'coursespending'));
80 print_header($strtitle,$strheading,build_navigation(array(array('name'=>$strheading,'link'=>'','type'=>'misc'))));
82 if (!empty($reject) and confirm_sesskey()) {
83 if ($reject = get_record("course_request","id",$reject)) {
84 if (empty($rejectnotice)) {
85 // display a form for writing a reason
86 print_simple_box_start('center');
87 print_string('courserejectreason');
88 include('pending-reject.html');
89 print_simple_box_end();
92 $user = get_record("user","id",$reject->requester
);
93 email_to_user($user,$USER,get_string('courserejectsubject'),get_string('courserejectemail','moodle',$rejectnotice));
94 delete_records("course_request","id",$reject->id
);
95 notice(get_string('courserejected'),'pending.php');
98 } else if ($pending = get_records("course_request")) {
100 $table->cellpadding
= 4;
101 $table->cellspacing
= 3;
102 $table->align
= array('center','center','center','center','center','center','center');
103 $table->head
= array(' ',get_string('shortname'),get_string('fullname'),get_string('requestedby'),get_string('summary'),
104 get_string('requestreason'),'');
105 $strrequireskey = get_string('requireskey');
106 foreach ($pending as $course) {
107 $requester = get_record('user','id',$course->requester
);
108 // check here for shortname collisions and warn about them.
109 if ($match = get_record("course","shortname",$course->shortname
)) {
110 $course->shortname
.= ' [*]';
113 //do not output raw html from request, quote html entities using s()!!
114 $table->data
[] = array(((!empty($course->password
)) ?
115 '<img hspace="1" alt="'.$strrequireskey.'" class="icon" src="'.$CFG->pixpath
.'/i/key.gif" />' : ''),
116 format_string($course->shortname
),format_string($course->fullname
),fullname($requester),
117 format_string($course->summary
),format_string($course->reason
),
118 '<a href="pending.php?approve='.$course->id
.'&sesskey='.sesskey().'">'.get_string('approve').'</a> | '
119 .'<a href="pending.php?reject='.$course->id
.'&sesskey='.sesskey().'">'.get_string('reject').'</a>');
122 if (!empty($collision)) {
123 print_string('shortnamecollisionwarning');
126 notice(get_string('nopendingcourses'));
127 // no pending messages.