insert calls to require_capability()
[moodle.git] / mod / feedback / mapcourse.php
CommitLineData
84973212 1<?php
2
c70ad9f7 3/**
84973212 4 * print the form to map courses for global feedbacks
5 *
6 * @author Andreas Grabs
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package feedback
9 */
10
11require_once("../../config.php");
12require_once("lib.php");
13require_once("$CFG->libdir/tablelib.php");
14
15$id = required_param('id', PARAM_INT); // Course Module ID, or
16$searchcourse = optional_param('searchcourse', '', PARAM_ALPHANUM);
17$coursefilter = optional_param('coursefilter', '', PARAM_INT);
18$courseid = optional_param('courseid', false, PARAM_INT);
19
a6855934 20$url = new moodle_url('/mod/feedback/mapcourse.php', array('id'=>$id));
84973212 21if ($searchcourse !== '') {
22 $url->param('searchcourse', $searchcourse);
23}
24if ($coursefilter !== '') {
25 $url->param('coursefilter', $coursefilter);
26}
27if ($courseid !== false) {
28 $url->param('courseid', $courseid);
29}
30$PAGE->set_url($url);
31
32if(($formdata = data_submitted()) AND !confirm_sesskey()) {
33 print_error('invalidsesskey');
34}
35
36// $SESSION->feedback->current_tab = 'mapcourse';
37$current_tab = 'mapcourse';
38
39if ($id) {
40 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
41 print_error('invalidcoursemodule');
c70ad9f7 42 }
d4b1d58c 43
84973212 44 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
45 print_error('coursemisconf');
c70ad9f7 46 }
d4b1d58c 47
84973212 48 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
49 print_error('invalidcoursemodule');
c70ad9f7 50 }
84973212 51}
c987dbad
AG
52
53if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
54 print_error('badcontext');
55}
84973212 56
57require_login($course->id, true, $cm);
58
53ba55ec 59require_capability('mod/feedback:mapcourse', $context);
84973212 60
61if ($coursefilter) {
62 $map->feedbackid = $feedback->id;
63 $map->courseid = $coursefilter;
64 // insert a map only if it does exists yet
65 $sql = "SELECT id, feedbackid
66 FROM {feedback_sitecourse_map}
67 WHERE feedbackid = ? AND courseid = ?";
68 if (!$DB->get_records_sql($sql, array($map->feedbackid, $map->courseid))) {
69 $DB->insert_record('feedback_sitecourse_map', $map);
c70ad9f7 70 }
84973212 71}
72
73/// Print the page header
74$strfeedbacks = get_string("modulenameplural", "feedback");
75$strfeedback = get_string("modulename", "feedback");
76
a6855934 77$PAGE->navbar->add($strfeedbacks, new moodle_url('/mod/feedback/index.php', array('id'=>$course->id)));
84973212 78$PAGE->navbar->add(format_string($feedback->name));
79
80$PAGE->set_title(format_string($feedback->name));
84973212 81echo $OUTPUT->header();
82
83include('tabs.php');
84
85echo $OUTPUT->box(get_string('mapcourseinfo', 'feedback'), 'generalbox boxaligncenter boxwidthwide');
86echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
87echo '<form method="post">';
88echo '<input type="hidden" name="id" value="'.$id.'" />';
89echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
90
91$sql = "select c.id, c.shortname
92 from {course} c
93 where c.shortname ".$DB->sql_ilike()." ?
94 OR c.fullname ".$DB->sql_ilike()." ?";
95$params = array("%{$searchcourse}%", "%{$searchcourse}%");
96
97if (($courses = $DB->get_records_sql_menu($sql, $params)) && !empty($searchcourse)) {
98 echo ' ' . get_string('courses') . ': ';
93af06be 99 echo html_writer::select($courses, 'coursefilter', $coursefilter);
84973212 100 echo '<input type="submit" value="'.get_string('mapcourse', 'feedback').'"/>';
4bcc5118 101 echo $OUTPUT->help_icon('mapcourses', '', 'feedback', true);
84973212 102 echo '<input type="button" value="'.get_string('searchagain').'" onclick="document.location=\'mapcourse.php?id='.$id.'\'"/>';
103 echo '<input type="hidden" name="searchcourse" value="'.$searchcourse.'"/>';
104 echo '<input type="hidden" name="feedbackid" value="'.$feedback->id.'"/>';
4bcc5118 105 echo $OUTPUT->help_icon('searchcourses', '', 'feedback', true);
84973212 106} else {
107 echo '<input type="text" name="searchcourse" value="'.$searchcourse.'"/> <input type="submit" value="'.get_string('searchcourses').'"/>';
4bcc5118 108 echo $OUTPUT->help_icon('searchcourses', '', 'feedback', true);
84973212 109}
110
111echo '</form>';
112
113if($coursemap = feedback_get_courses_from_sitecourse_map($feedback->id)) {
114 $table = new flexible_table('coursemaps');
115 $table->define_columns( array('course'));
116 $table->define_headers( array(get_string('mappedcourses', 'feedback')));
117
118 $table->setup();
119
5dee2210 120 $unmapurl = new moodle_url('/mod/feedback/unmapcourse.php');
84973212 121 foreach ($coursemap as $cmap) {
5dee2210
AG
122 $unmapurl->params(array('id'=>$id, 'cmapid'=>$cmap->id));
123 $table->add_data(array('<a href="'.$unmapurl->out().'"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="Delete" /></a> ('.$cmap->shortname.') '.$cmap->fullname));
c70ad9f7 124 }
d4b1d58c 125
84973212 126 $table->print_html();
127} else {
128 echo '<h3>'.get_string('mapcoursenone', 'feedback').'</h3>';
129}
d4b1d58c 130
131
84973212 132echo $OUTPUT->box_end();
d4b1d58c 133
84973212 134echo $OUTPUT->footer();
c70ad9f7 135