be710fe1e56eb12b04804a9f55aefbb42f39feb1
[moodle.git] / tag / coursetags_edit.php
1 <?php
3 /**
4  * coursetags_edit.php
5  * displays personal tags for a course with some editing facilites
6  * @author j.beedell@open.ac.uk June07
7  */
9 require_once('../config.php');
10 require_once($CFG->dirroot.'/tag/coursetagslib.php');
11 require_once($CFG->dirroot.'/tag/lib.php');
13 $courseid = optional_param('courseid', 0, PARAM_INT);
14 $keyword = optional_param('coursetag_new_tag', '', PARAM_TEXT);
15 $deltag = optional_param('del_tag', 0, PARAM_INT);
17 $url = new moodle_url('/tag/coursetags_edit.php');
18 if ($courseid !== 0) {
19     $url->param('courseid', $courseid);
20 }
21 if ($keyword !== '') {
22     $url->param('coursetag_new_tag', $keyword);
23 }
24 if ($deltag !== 0) {
25     $url->param('del_tag', $deltag);
26 }
27 $PAGE->set_url($url);
29 require_login();
31 if (empty($CFG->usetags)) {
32     print_error('tagsaredisabled', 'tag');
33 }
35 if ($courseid != SITEID) {
36     if (! ($course = $DB->get_record('course', array('id' => $courseid), '*')) ) {
37         print_error('invalidcourse');
38     }
39 } else {
40     print_error('errortagfrontpage', 'tag');
41 }
43 // Permissions
44 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
45 require_login($course->id);
46 $canedit = has_capability('moodle/tag:create', $sitecontext);
48 // Language strings
49 $tagslang = 'block_tags';
51 // Store data
52 if ($data = data_submitted()) {
53     if (confirm_sesskey() and $courseid > 0 and $USER->id > 0 and $canedit) {
54         // store personal tag
55         if (trim(strip_tags($keyword))) {
56             $myurl = 'tag/search.php';
57             $keywords = explode(',', $keyword);
58             coursetag_store_keywords($keywords, $courseid, $USER->id, 'default', $myurl);
59         }
60         // delete personal tag
61         if ($deltag > 0) {
62             coursetag_delete_keyword($deltag, $USER->id, $courseid);
63         }
64     }
65 }
67 // The title and breadcrumb
68 $title = get_string('edittitle', $tagslang);
69 $coursefullname = format_string($course->fullname);
70 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
71 $PAGE->navbar->add($title);
72 $PAGE->set_title($title);
73 $PAGE->set_heading($course->fullname);
74 $PAGE->set_cacheable(false);
75 echo $OUTPUT->header();
77     // Print personal tags for all courses
78     $title = get_string('edittitle', $tagslang);
79     echo $OUTPUT->heading($title, 2, 'mdl-align');
81     $mytags = coursetag_print_cloud(coursetag_get_tags(0, $USER->id, 'default'), true);
82     $outstr = '
83         <div class="coursetag_edit_centered">
84             <div>
85                 '.get_string('editmytags', $tagslang).'
86             </div>
87             <div>';
89     if ($mytags) {
90         $outstr .= $mytags;
91     } else {
92         $outstr .= get_string('editnopersonaltags', $tagslang);
93     }
95     $outstr .= '
96             </div>
97         </div>';
98     echo $outstr;
100     // Personal tag editing
101     if ($canedit) {
102         $title = get_string('editmytagsfor', $tagslang, '"'.$coursefullname.' ('.$courseshortname.')"');
103         echo $OUTPUT->heading($title, 2, 'main mdl-align');
105         // Deletion here is open to the users own tags for this course only
106         $selectoptions = '<option value="0">'.get_string('select', $tagslang).'</option>';
107         $coursetabs = '';
108         if ($options = coursetag_get_records($courseid, $USER->id)) {
109             $coursetabs = '"';
110             foreach ($options as $option) {
111                 $selectoptions .= '<option value="'.$option->id.'">'.$option->rawname.'</option>';
112                 $coursetabs .= $option->rawname . ', ';
113             }
114             $coursetabs = rtrim($coursetabs, ', ');
115             $coursetabs .= '"';
116         }
117         if ($coursetabs) {
118             $outstr = '
119             <div class="coursetag_edit_centered">
120                 '.get_string('editthiscoursetags', $tagslang, $coursetabs).'
121             </div>';
122         } else {
123             $outstr = '
124             <div class="coursetag_edit_centered">
125                 '.get_string('editnopersonaltags', $tagslang).'
126             </div>';
127         }
129         // Print the add and delete form
130         coursetag_get_jscript();
131         $edittagthisunit = get_string('edittagthisunit', $tagslang);
132         $arrowtitle = get_string('arrowtitle', $tagslang);
133         $sesskey = sesskey();
134         $leftarrow = $OUTPUT->pix_url('t/arrow_left');
135         $outstr .= <<<EOT
136             <form action="$CFG->wwwroot/tag/coursetags_edit.php" method="post" id="coursetag">
137                 <div style="display: none;">
138                     <input type="hidden" name="courseid" value="$course->id" />
139                     <input type="hidden" name="sesskey" value="$sesskey" />
140                 </div>
141                 <div class="coursetag_edit_centered">
142                     <div class="coursetag_edit_row">
143                         <div class="coursetag_edit_left">
144                             $edittagthisunit
145                         </div>
146                         <div class="coursetag_edit_right">
147                             <div class="coursetag_form_input1">
148                                 <input type="text" name="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
149                             </div>
150                             <div class="coursetag_form_input2">
151                                 <input type="text" name="coursetag_new_tag" id="coursetag_new_tag" class="coursetag_form_input2a"
152                                     onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
153                             </div>
154                             <div class="coursetag_edit_input3" id="coursetag_sug_btn">
155                                 <a title="$arrowtitle">
156                                     <img src="$leftarrow" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
157                                 </a>
158                             </div>
159                         </div>
160                     </div>
161 EOT;
162         if ($coursetabs) {
163             $editdeletemytag = get_string('editdeletemytag', $tagslang);
164             $outstr .= <<<EOT1
165                     <div class="coursetag_edit_row">
166                         <div class="coursetag_edit_left">
167                             $editdeletemytag
168                         </div>
169                         <div class="coursetag_edit_right">
170                             <select name="del_tag">
171                                 $selectoptions
172                             </select>
173                         </div>
174                     </div>
175 EOT1;
176         }
177         $submitstr = get_string('submit');
178         $outstr .= <<<EOT2
179                     <div class="clearer"></div>
180                     <div class="coursetag_edit_row">
181                         <button type="submit">$submitstr</button>
182                     </div>
183                 </div>
184             </form>
185 EOT2;
186         echo $outstr;
187     }
189 echo $OUTPUT->footer();