~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to question/editlib.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php // $Id: editlib.php,v 1.41.2.12 2007/05/22 13:24:47 tjhunt Exp $
 
1
<?php // $Id: editlib.php,v 1.76.2.10 2008/11/27 11:50:20 tjhunt Exp $
2
2
/**
3
 
* Functions used by showbank.php to show question editing interface
4
 
*
5
 
* TODO: currently the function question_list still provides controls specific
6
 
*       to the quiz module. This needs to be generalised.
7
 
*
8
 
* @version $Id: editlib.php,v 1.41.2.12 2007/05/22 13:24:47 tjhunt Exp $
9
 
* @author Martin Dougiamas and many others. This has recently been extensively
10
 
*         rewritten by members of the Serving Mathematics project
11
 
*         {@link http://maths.york.ac.uk/serving_maths}
12
 
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13
 
* @package question
14
 
*/
 
3
 * Functions used to show question editing interface
 
4
 *
 
5
 *
 
6
 * @author Martin Dougiamas and many others. This has recently been extensively
 
7
 *         rewritten by members of the Serving Mathematics project
 
8
 *         {@link http://maths.york.ac.uk/serving_maths}
 
9
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 
10
 * @package questionbank
 
11
 */
15
12
 
16
13
require_once($CFG->libdir.'/questionlib.php');
17
14
 
18
15
define('DEFAULT_QUESTIONS_PER_PAGE', 20);
19
16
 
 
17
function get_module_from_cmid($cmid){
 
18
    global $CFG;
 
19
    if (!$cmrec = get_record_sql("SELECT cm.*, md.name as modname
 
20
                               FROM {$CFG->prefix}course_modules cm,
 
21
                                    {$CFG->prefix}modules md
 
22
                               WHERE cm.id = '$cmid' AND
 
23
                                     md.id = cm.module")){
 
24
        error('cmunknown');
 
25
    } elseif (!$modrec =get_record($cmrec->modname, 'id', $cmrec->instance)) {
 
26
        error('cmunknown');
 
27
    }
 
28
    $modrec->instance = $modrec->id;
 
29
    $modrec->cmid = $cmrec->id;
 
30
    $cmrec->name = $modrec->name;
 
31
 
 
32
    return array($modrec, $cmrec);
 
33
}
20
34
/**
21
35
* Function to read all questions for category into big array
22
36
*
23
37
* @param int $category category number
24
38
* @param bool $noparent if true only questions with NO parent will be selected
25
39
* @param bool $recurse include subdirectories
 
40
* @param bool $export set true if this is called by questionbank export
26
41
* @author added by Howard Miller June 2004
27
42
*/
28
43
function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
62
77
}
63
78
 
64
79
/**
65
 
* Gets the default category in a course
66
 
*
67
 
* It returns the first category with no parent category. If no categories
68
 
* exist yet then one is created.
69
 
* @return object The default category
70
 
* @param integer $courseid  The id of the course whose default category is wanted
71
 
*/
72
 
function get_default_question_category($courseid) {
73
 
    // If it already exists, just return it.
74
 
    if ($category = get_records_select("question_categories", "course = '$courseid' AND parent = '0'", 'id', '*', '', 1)) {
75
 
        return reset($category);
76
 
    }
77
 
 
78
 
    // Otherwise, we need to make one
79
 
    $category = new stdClass;
80
 
    $category->name = get_string("default", "quiz");
81
 
    $category->info = get_string("defaultinfo", "quiz");
82
 
    $category->course = $courseid;
83
 
    $category->parent = 0;
84
 
    $category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically.
85
 
    $category->publish = 0;
86
 
    $category->stamp = make_unique_id_code();
87
 
 
88
 
    if (!$category->id = insert_record("question_categories", $category)) {
89
 
        notify("Error creating a default category!");
90
 
        return false;
91
 
    }
92
 
    return $category;
93
 
}
94
 
 
 
80
 * @param integer $categoryid a category id.
 
81
 * @return boolean whether this is the only top-level category in a context.
 
82
 */
 
83
function question_is_only_toplevel_category_in_context($categoryid) {
 
84
    global $CFG;
 
85
    return 1 == count_records_sql("
 
86
            SELECT count(*)
 
87
              FROM {$CFG->prefix}question_categories c1,
 
88
                   {$CFG->prefix}question_categories c2
 
89
             WHERE c2.id = $categoryid
 
90
               AND c1.contextid = c2.contextid
 
91
               AND c1.parent = 0 AND c2.parent = 0");
 
92
}
 
93
 
 
94
/**
 
95
 * Check whether this user is allowed to delete this category.
 
96
 *
 
97
 * @param integer $todelete a category id.
 
98
 */
 
99
function question_can_delete_cat($todelete) {
 
100
    if (question_is_only_toplevel_category_in_context($todelete)) {
 
101
        error('You can\'t delete that category it is the default category for this context.');
 
102
    } else {
 
103
        $contextid = get_field('question_categories', 'contextid', 'id', $todelete);
 
104
        require_capability('moodle/question:managecategory', get_context_instance_by_id($contextid));
 
105
    }
 
106
}
95
107
/**
96
108
 * prints a form to choose categories
97
109
 */
98
 
function question_category_form($course, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
 
110
function question_category_form($contexts, $pageurl, $current, $recurse=1, $showhidden=false, $showquestiontext=false) {
99
111
    global $CFG;
100
112
 
101
 
/// Make sure the default category exists for this course
102
 
    get_default_question_category($course->id);
103
113
 
104
114
/// Get all the existing categories now
105
 
    $catmenu = question_category_options($course->id, true);
106
 
 
107
 
    $strcategory = get_string("category", "quiz");
108
 
    $strshow = get_string("show", "quiz");
109
 
    $streditcats = get_string("editcategories", "quiz");
110
 
 
111
 
    echo "<table><tr><td style=\"white-space:nowrap;\">";
112
 
    echo "<strong>$strcategory:</strong>&nbsp;";
113
 
    echo "</td><td>";
114
 
    popup_form ("edit.php?courseid=$course->id&amp;cat=", $catmenu, "catmenu", $current, "", "", "", false, "self");
115
 
    echo "</td><td align=\"right\">";
116
 
    echo "<form method=\"get\" action=\"$CFG->wwwroot/question/category.php\">";
117
 
    echo "<div>";
118
 
    echo "<input type=\"hidden\" name=\"id\" value=\"$course->id\" />";
119
 
    echo "<input type=\"submit\" value=\"$streditcats\" />";
120
 
    echo '</div>';
121
 
    echo "</form>";
122
 
    echo '</td></tr></table>';
 
115
    $catmenu = question_category_options($contexts, false, 0, true);
 
116
 
 
117
    $strcategory = get_string('category', 'quiz');
 
118
    $strshow = get_string('show', 'quiz');
 
119
    $streditcats = get_string('editcategories', 'quiz');
 
120
 
 
121
    popup_form ('edit.php?'.$pageurl->get_query_string().'&amp;category=', $catmenu, 'catmenu', $current, '', '', '', false, 'self', "<strong>$strcategory</strong>");
123
122
 
124
123
    echo '<form method="get" action="edit.php" id="displayoptions">';
125
124
    echo "<fieldset class='invisiblefieldset'>";
126
 
    echo "<input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />\n";
 
125
    echo $pageurl->hidden_params_out(array('recurse', 'showhidden', 'showquestiontext'));
127
126
    question_category_form_checkbox('recurse', $recurse);
128
127
    question_category_form_checkbox('showhidden', $showhidden);
129
128
    question_category_form_checkbox('showquestiontext', $showquestiontext);
151
150
*
152
151
* @param object $course   The course object
153
152
* @param int $categoryid  The id of the question category to be displayed
154
 
* @param int $quizid      The quiz id if we are in the context of a particular quiz, 0 otherwise
 
153
* @param int $cm      The course module record if we are in the context of a particular module, 0 otherwise
155
154
* @param int $recurse     This is 1 if subcategories should be included, 0 otherwise
156
155
* @param int $page        The number of the page to be displayed
157
156
* @param int $perpage     Number of questions to show per page
158
157
* @param boolean $showhidden   True if also hidden questions should be displayed
159
158
* @param boolean $showquestiontext whether the text of each question should be shown in the list
160
159
*/
161
 
function question_list($course, $categoryid, $quizid=0,
162
 
        $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC',
163
 
        $showquestiontext = false) {
164
 
    global $QTYPE_MENU, $USER, $CFG, $THEME;
165
 
    
166
 
    $qtypemenu = $QTYPE_MENU;
167
 
    if ($rqp_types = get_records('question_rqp_types')) {
168
 
        foreach($rqp_types as $type) {
169
 
            $qtypemenu['rqp_'.$type->id] = $type->name;
170
 
        }
171
 
    }
 
160
function question_list($contexts, $pageurl, $categoryandcontext, $cm = null,
 
161
        $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='typename', $sortorderdecoded='qtype, name ASC',
 
162
        $showquestiontext = false, $addcontexts = array()) {
 
163
    global $USER, $CFG, $THEME, $COURSE;
 
164
 
 
165
    list($categoryid, $contextid)=  explode(',', $categoryandcontext);
 
166
 
 
167
    $qtypemenu = question_type_menu();
172
168
 
173
169
    $strcategory = get_string("category", "quiz");
174
170
    $strquestion = get_string("question", "quiz");
183
179
    $strquestionname = get_string("questionname", "quiz");
184
180
    $strdelete = get_string("delete");
185
181
    $stredit = get_string("edit");
 
182
    $strmove = get_string('moveqtoanothercontext', 'question');
 
183
    $strview = get_string("view");
186
184
    $straction = get_string("action");
187
185
    $strrestore = get_string('restore');
188
186
 
189
 
    $straddtoquiz = get_string("addtoquiz", "quiz");
190
187
    $strtype = get_string("type", "quiz");
191
188
    $strcreatemultiple = get_string("createmultiple", "quiz");
192
189
    $strpreview = get_string("preview","quiz");
195
192
        echo "<p style=\"text-align:center;\"><b>";
196
193
        print_string("selectcategoryabove", "quiz");
197
194
        echo "</b></p>";
198
 
        if ($quizid) {
199
 
            echo "<p>";
200
 
            print_string("addingquestions", "quiz");
201
 
            echo "</p>";
202
 
        }
203
195
        return;
204
196
    }
205
197
 
206
 
    if (!$category = get_record('question_categories', 'id', $categoryid)) {
 
198
    if (!$category = get_record('question_categories', 'id', $categoryid, 'contextid', $contextid)) {
207
199
        notify('Category not found!');
208
200
        return;
209
201
    }
210
 
    $canedit = has_capability('moodle/question:manage', get_context_instance(CONTEXT_COURSE, $category->course));
211
 
    $editingquiz = false;
212
 
    if ($quizid) {
213
 
        $cm = get_coursemodule_from_instance('quiz', $quizid);
214
 
        $editingquiz = has_capability('mod/quiz:manage', get_context_instance(CONTEXT_MODULE, $cm->id));
215
 
    }
216
 
    
 
202
    $catcontext = get_context_instance_by_id($contextid);
 
203
    $canadd = has_capability('moodle/question:add', $catcontext);
 
204
    //check for capabilities on all questions in category, will also apply to sub cats.
 
205
    $caneditall =has_capability('moodle/question:editall', $catcontext);
 
206
    $canuseall =has_capability('moodle/question:useall', $catcontext);
 
207
    $canmoveall =has_capability('moodle/question:moveall', $catcontext);
 
208
 
 
209
    if ($cm AND $cm->modname == 'quiz') {
 
210
        $quizid = $cm->instance;
 
211
    } else {
 
212
        $quizid = 0;
 
213
    }
 
214
    $returnurl = $pageurl->out();
 
215
    $questionurl = new moodle_url("$CFG->wwwroot/question/question.php",
 
216
                                array('returnurl' => $returnurl));
 
217
    if ($cm!==null){
 
218
        $questionurl->param('cmid', $cm->id);
 
219
    } else {
 
220
        $questionurl->param('courseid', $COURSE->id);
 
221
    }
 
222
    $questionmoveurl = new moodle_url("$CFG->wwwroot/question/contextmoveq.php",
 
223
                                array('returnurl' => $returnurl));
 
224
    if ($cm!==null){
 
225
        $questionmoveurl->param('cmid', $cm->id);
 
226
    } else {
 
227
        $questionmoveurl->param('courseid', $COURSE->id);
 
228
    }
217
229
    echo '<div class="boxaligncenter">';
218
230
    $formatoptions = new stdClass;
219
231
    $formatoptions->noclean = true;
220
 
    echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $course->id);
 
232
    echo format_text($category->info, FORMAT_MOODLE, $formatoptions, $COURSE->id);
221
233
 
222
234
    echo '<table><tr>';
223
235
 
224
 
    // check if editing questions in this category is allowed
225
 
    if ($canedit) {
226
 
        echo "<td valign=\"top\"><b>$strcreatenewquestion:</b></td>";
 
236
    if ($canadd) {
227
237
        echo '<td valign="top" align="right">';
228
 
        popup_form ("$CFG->wwwroot/question/question.php?category=$category->id&amp;qtype=", $qtypemenu, "addquestion",
229
 
                    "", "choose", "", "", false, "self");
 
238
        popup_form ($questionurl->out(false, array('category' => $category->id)).'&amp;qtype=', $qtypemenu, "addquestion", "", "choose", "", "", false, "self", "<strong>$strcreatenewquestion</strong>");
230
239
        echo '</td><td valign="top" align="right">';
231
240
        helpbutton("questiontypes", $strcreatenewquestion, "quiz");
232
241
        echo '</td>';
233
242
    }
234
243
    else {
235
244
        echo '<td>';
236
 
        print_string("publishedit","quiz");
 
245
        print_string('nopermissionadd', 'question');
237
246
        echo '</td>';
238
247
    }
239
248
 
252
261
        return;
253
262
    }
254
263
 
255
 
    if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', $page*$perpage, $perpage)) {
 
264
    if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', $page*$perpage, $perpage)) {
256
265
        // There are no questions on the requested page.
257
266
        $page = 0;
258
 
        if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorder, '*', 0, $perpage)) {
 
267
        if (!$questions = get_records_select('question', "category IN ($categorylist) AND parent = '0' $showhidden", $sortorderdecoded, '*', 0, $perpage)) {
259
268
            // There are no questions at all
260
269
            echo "<p style=\"text-align:center;\">";
261
270
            print_string("noquestions", "quiz");
264
273
        }
265
274
    }
266
275
 
267
 
    print_paging_bar($totalnumber, $page, $perpage,
268
 
                "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;");
269
 
 
270
 
    echo '<form method="post" action="edit.php?courseid='.$course->id.'">';
 
276
    print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage');
 
277
 
 
278
    echo question_sort_options($pageurl, $sortorder);
 
279
 
 
280
 
 
281
    echo '<form method="post" action="edit.php">';
271
282
    echo '<fieldset class="invisiblefieldset" style="display: block;">';
272
283
    echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
273
 
 
 
284
    echo $pageurl->hidden_params_out();
274
285
    echo '<table id="categoryquestions" style="width: 100%"><tr>';
275
286
    echo "<th style=\"white-space:nowrap;\" class=\"header\" scope=\"col\">$straction</th>";
276
 
    
277
 
    $sortoptions = array('name, qtype ASC' => get_string("sortalpha", "quiz"),
278
 
                         'qtype, name ASC' => get_string("sorttypealpha", "quiz"),
279
 
                         'id ASC' => get_string("sortage", "quiz"));
280
 
    $orderselect  = choose_from_menu ($sortoptions, 'sortorder', $sortorder, false, 'this.form.submit();', '0', true);
281
 
    $orderselect .= '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
282
 
    echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname $orderselect</th>
 
287
 
 
288
    echo "<th style=\"white-space:nowrap; text-align: left;\" class=\"header\" scope=\"col\">$strquestionname</th>
283
289
    <th style=\"white-space:nowrap; text-align: right;\" class=\"header\" scope=\"col\">$strtype</th>";
284
290
    echo "</tr>\n";
285
291
    foreach ($questions as $question) {
298
304
        if ($textclass) {
299
305
            $textclass = 'class="' . $textclass . '"';
300
306
        }
301
 
        
 
307
 
302
308
        echo "<tr>\n<td style=\"white-space:nowrap;\" $nameclass>\n";
303
 
        
304
 
        // add to quiz
305
 
        if ($editingquiz) {
306
 
            echo "<a title=\"$straddtoquiz\" href=\"edit.php?addquestion=$question->id&amp;quizid=$quizid&amp;sesskey=$USER->sesskey\"><img
307
 
                  src=\"$CFG->pixpath/t/moveleft.gif\" alt=\"$straddtoquiz\" /></a>&nbsp;";
 
309
 
 
310
        $canuseq = question_has_capability_on($question, 'use', $question->category);
 
311
        if (function_exists('module_specific_actions')) {
 
312
            echo module_specific_actions($pageurl, $question->id, $cm->id, $canuseq);
308
313
        }
309
 
        
 
314
 
310
315
        // preview
311
 
        link_to_popup_window('/question/preview.php?id=' . $question->id . '&amp;quizid=' . $quizid, 'questionpreview',
312
 
                "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />",
313
 
                0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS);
314
 
        
 
316
        if ($canuseq) {
 
317
            $quizorcourseid = $quizid?('&amp;quizid=' . $quizid):('&amp;courseid=' .$COURSE->id);
 
318
            link_to_popup_window('/question/preview.php?id=' . $question->id . $quizorcourseid, 'questionpreview',
 
319
                    "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strpreview\" />",
 
320
                    0, 0, $strpreview, QUESTION_PREVIEW_POPUP_OPTIONS);
 
321
        }
315
322
        // edit, hide, delete question, using question capabilities, not quiz capabilieies
316
 
        if ($canedit) {
317
 
            echo "<a title=\"$stredit\" href=\"$CFG->wwwroot/question/question.php?id=$question->id\"><img
 
323
        if (question_has_capability_on($question, 'edit', $question->category) || question_has_capability_on($question, 'move', $question->category)) {
 
324
            echo "<a title=\"$stredit\" href=\"".$questionurl->out(false, array('id'=>$question->id))."\"><img
318
325
                    src=\"$CFG->pixpath/t/edit.gif\" alt=\"$stredit\" /></a>&nbsp;";
 
326
        } elseif (question_has_capability_on($question, 'view', $question->category)){
 
327
            echo "<a title=\"$strview\" href=\"".$questionurl->out(false, array('id'=>$question->id))."\"><img
 
328
                    src=\"$CFG->pixpath/i/info.gif\" alt=\"$strview\" /></a>&nbsp;";
 
329
        }
 
330
 
 
331
        if (question_has_capability_on($question, 'move', $question->category) && question_has_capability_on($question, 'view', $question->category)) {
 
332
            echo "<a title=\"$strmove\" href=\"".$questionurl->out(false, array('id'=>$question->id, 'movecontext'=>1))."\"><img
 
333
                    src=\"$CFG->pixpath/t/move.gif\" alt=\"$strmove\" /></a>&nbsp;";
 
334
        }
 
335
 
 
336
        if (question_has_capability_on($question, 'edit', $question->category)) {
319
337
            // hide-feature
320
338
            if($question->hidden) {
321
 
                echo "<a title=\"$strrestore\" href=\"edit.php?courseid=$course->id&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
 
339
                echo "<a title=\"$strrestore\" href=\"edit.php?".$pageurl->get_query_string()."&amp;unhide=$question->id&amp;sesskey=$USER->sesskey\"><img
322
340
                        src=\"$CFG->pixpath/t/restore.gif\" alt=\"$strrestore\" /></a>";
323
341
            } else {
324
 
                echo "<a title=\"$strdelete\" href=\"edit.php?courseid=$course->id&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
 
342
                echo "<a title=\"$strdelete\" href=\"edit.php?".$pageurl->get_query_string()."&amp;deleteselected=$question->id&amp;q$question->id=1\"><img
325
343
                        src=\"$CFG->pixpath/t/delete.gif\" alt=\"$strdelete\" /></a>";
326
344
            }
327
345
        }
328
 
        echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
 
346
        if ($caneditall || $canmoveall || $canuseall){
 
347
            echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
 
348
        }
329
349
        echo "</td>\n";
330
350
 
331
351
        echo "<td $nameclass>" . format_string($question->name) . "</td>\n";
339
359
            $formatoptions->noclean = true;
340
360
            $formatoptions->para = false;
341
361
            echo format_text($question->questiontext, $question->questiontextformat,
342
 
                    $formatoptions, $course->id);
 
362
                    $formatoptions, $COURSE->id);
343
363
            echo "</td></tr>\n";
344
364
        }
345
365
    }
346
366
    echo "</table>\n";
347
367
 
348
 
    $paging = print_paging_bar($totalnumber, $page, $perpage,
349
 
            "edit.php?courseid={$course->id}&amp;perpage=$perpage&amp;", 'page',
350
 
            false, true);
 
368
    $paging = print_paging_bar($totalnumber, $page, $perpage, $pageurl, 'qpage', false, true);
351
369
    if ($totalnumber > DEFAULT_QUESTIONS_PER_PAGE) {
352
370
        if ($perpage == DEFAULT_QUESTIONS_PER_PAGE) {
353
 
            $showall = '<a href="edit.php?courseid='.$course->id.'&amp;perpage=1000">'.get_string('showall', 'moodle', $totalnumber).'</a>';
 
371
            $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>1000)).'">'.get_string('showall', 'moodle', $totalnumber).'</a>';
354
372
        } else {
355
 
            $showall = '<a href="edit.php?courseid='.$course->id.'&amp;perpage=' . DEFAULT_QUESTIONS_PER_PAGE . '">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).'</a>';
 
373
            $showall = '<a href="edit.php?'.$pageurl->get_query_string(array('qperpage'=>DEFAULT_QUESTIONS_PER_PAGE)).'">'.get_string('showperpage', 'moodle', DEFAULT_QUESTIONS_PER_PAGE).'</a>';
356
374
        }
357
375
        if ($paging) {
358
376
            $paging = substr($paging, 0, strrpos($paging, '</div>'));
359
377
            $paging .= "<br />$showall</div>";
360
378
        } else {
361
 
            $paging = "<div class='paging'>$showall</div>"; 
 
379
            $paging = "<div class='paging'>$showall</div>";
362
380
        }
363
381
    }
364
382
    echo $paging;
365
383
 
366
 
    echo '<table class="quiz-edit-selected"><tr><td colspan="2">';
367
 
    echo '<a href="javascript:select_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectall.'</a> /'.
368
 
     ' <a href="javascript:deselect_all_in(\'TABLE\', null, \'categoryquestions\');">'.$strselectnone.'</a>'.
369
 
     '</td><td align="right"><b>&nbsp;'.get_string('withselected', 'quiz').':</b></td></tr><tr><td>';
370
 
 
371
 
    if ($editingquiz) {
372
 
        echo "<input type=\"submit\" name=\"add\" value=\"{$THEME->larrow} $straddtoquiz\" />\n";
373
 
        echo '</td><td>';
374
 
    }
375
 
    // print delete and move selected question
376
 
    if ($canedit) {
377
 
        echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" /></td><td>\n";
378
 
        echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
379
 
        question_category_select_menu($course->id, false, true, $category->id);
380
 
    }
381
 
    echo "</td></tr></table>";
382
 
 
383
 
    // add random question
384
 
    if ($editingquiz) {
385
 
        for ($i = 1;$i <= min(10, $totalnumber); $i++) {
386
 
            $randomcount[$i] = $i;
387
 
        }
388
 
        for ($i = 20;$i <= min(100, $totalnumber); $i += 10) {
389
 
            $randomcount[$i] = $i;
390
 
        }
 
384
    if ($caneditall || $canmoveall || $canuseall){
 
385
        echo '<a href="javascript:select_all_in(\'TABLE\',null,\'categoryquestions\');">'.$strselectall.'</a> /'.
 
386
         ' <a href="javascript:deselect_all_in(\'TABLE\',null,\'categoryquestions\');">'.$strselectnone.'</a>';
391
387
        echo '<br />';
392
 
        print_string('addrandom', 'quiz',
393
 
         choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
394
 
        echo '<input type="hidden" name="recurse" value="'.$recurse.'" />';
395
 
        echo "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
396
 
        echo ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
397
 
        helpbutton('random', get_string('random', 'quiz'), 'quiz');
 
388
        echo '<strong>&nbsp;'.get_string('withselected', 'quiz').':</strong><br />';
 
389
 
 
390
        if (function_exists('module_specific_buttons')) {
 
391
            echo module_specific_buttons($cm->id);
 
392
        }
 
393
        // print delete and move selected question
 
394
        if ($caneditall) {
 
395
            echo '<input type="submit" name="deleteselected" value="'.$strdelete."\" />\n";
 
396
        }
 
397
        if ($canmoveall && count($addcontexts)) {
 
398
            echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
 
399
            question_category_select_menu($addcontexts, false, 0, "$category->id,$category->contextid");
 
400
        }
 
401
 
 
402
        if (function_exists('module_specific_controls') && $canuseall) {
 
403
            echo module_specific_controls($totalnumber, $recurse, $category, $cm->id);
 
404
        }
398
405
    }
399
406
    echo '</fieldset>';
400
407
    echo "</form>\n";
401
408
}
402
 
 
403
 
?>
 
 
b'\\ No newline at end of file'
 
409
function question_sort_options($pageurl, $sortorder){
 
410
    global $USER;
 
411
    //sort options
 
412
    $html = "<div class=\"mdl-align\">";
 
413
    $html .= '<form method="post" action="edit.php">';
 
414
    $html .= '<fieldset class="invisiblefieldset" style="display: block;">';
 
415
    $html .= '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
 
416
    $html .= $pageurl->hidden_params_out(array('qsortorder'));
 
417
    $sortoptions = array('alpha' => get_string("sortalpha", "quiz"),
 
418
                         'typealpha' => get_string("sorttypealpha", "quiz"),
 
419
                         'age' => get_string("sortage", "quiz"));
 
420
    $html .=  choose_from_menu ($sortoptions, 'qsortorder', $sortorder, false, 'this.form.submit();', '0', true);
 
421
    $html .=  '<noscript><div><input type="submit" value="'.get_string("sortsubmit", "quiz").'" /></div></noscript>';
 
422
    $html .= '</fieldset>';
 
423
    $html .= "</form>\n";
 
424
    $html .= "</div>\n";
 
425
    return $html;
 
426
}
 
427
 
 
428
function question_showbank_actions($pageurl, $cm){
 
429
    global $CFG, $COURSE;
 
430
    /// Now, check for commands on this page and modify variables as necessary
 
431
    if (optional_param('move', false, PARAM_BOOL) and confirm_sesskey()) { /// Move selected questions to new category
 
432
        $category = required_param('category', PARAM_SEQUENCE);
 
433
        list($tocategoryid, $contextid) = explode(',', $category);
 
434
        if (! $tocategory = get_record('question_categories', 'id', $tocategoryid, 'contextid', $contextid)) {
 
435
            error('Could not find category record');
 
436
        }
 
437
        $tocontext = get_context_instance_by_id($contextid);
 
438
        require_capability('moodle/question:add', $tocontext);
 
439
        $rawdata = (array) data_submitted();
 
440
        $questionids = array();
 
441
        foreach ($rawdata as $key => $value) {    // Parse input for question ids
 
442
            if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
 
443
                $key = $matches[1];
 
444
                $questionids[] = $key;
 
445
            }
 
446
        }
 
447
        if ($questionids){
 
448
            $questionidlist = join($questionids, ',');
 
449
            $sql = "SELECT q.*, c.contextid FROM {$CFG->prefix}question q, {$CFG->prefix}question_categories c WHERE q.id IN ($questionidlist) AND c.id = q.category";
 
450
            if (!$questions = get_records_sql($sql)){
 
451
                print_error('questiondoesnotexist', 'question', $pageurl->out());
 
452
            }
 
453
            $checkforfiles = false;
 
454
            foreach ($questions as $question){
 
455
                //check capabilities
 
456
                question_require_capability_on($question, 'move');
 
457
                $fromcontext = get_context_instance_by_id($question->contextid);
 
458
                if (get_filesdir_from_context($fromcontext) != get_filesdir_from_context($tocontext)){
 
459
                    $checkforfiles = true;
 
460
                }
 
461
            }
 
462
            $returnurl = $pageurl->out(false, array('category'=>"$tocategoryid,$contextid"));
 
463
            if (!$checkforfiles){
 
464
                if (!question_move_questions_to_category(implode(',', $questionids), $tocategory->id)) {
 
465
                    print_error('errormovingquestions', 'question', $returnurl, $questionids);
 
466
                }
 
467
                redirect($returnurl);
 
468
            } else {
 
469
                $movecontexturl  = new moodle_url($CFG->wwwroot.'/question/contextmoveq.php',
 
470
                                                array('returnurl' => $returnurl,
 
471
                                                        'ids'=>$questionidlist,
 
472
                                                        'tocatid'=> $tocategoryid));
 
473
                if ($cm){
 
474
                    $movecontexturl->param('cmid', $cm->id);
 
475
                } else {
 
476
                    $movecontexturl->param('courseid', $COURSE->id);
 
477
                }
 
478
                redirect($movecontexturl->out());
 
479
            }
 
480
        }
 
481
    }
 
482
 
 
483
    if (optional_param('deleteselected', false, PARAM_BOOL)) { // delete selected questions from the category
 
484
        if (($confirm = optional_param('confirm', '', PARAM_ALPHANUM)) and confirm_sesskey()) { // teacher has already confirmed the action
 
485
            $deleteselected = required_param('deleteselected');
 
486
            if ($confirm == md5($deleteselected)) {
 
487
                if ($questionlist = explode(',', $deleteselected)) {
 
488
                    // for each question either hide it if it is in use or delete it
 
489
                    foreach ($questionlist as $questionid) {
 
490
                        question_require_capability_on($questionid, 'edit');
 
491
                        if (record_exists('quiz_question_instances', 'question', $questionid)) {
 
492
                            if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
 
493
                                question_require_capability_on($questionid, 'edit');
 
494
                                error('Was not able to hide question');
 
495
                            }
 
496
                        } else {
 
497
                            delete_question($questionid);
 
498
                        }
 
499
                    }
 
500
                }
 
501
                redirect($pageurl->out());
 
502
            } else {
 
503
                error("Confirmation string was incorrect");
 
504
            }
 
505
        }
 
506
    }
 
507
 
 
508
    // Unhide a question
 
509
    if(($unhide = optional_param('unhide', '', PARAM_INT)) and confirm_sesskey()) {
 
510
        question_require_capability_on($unhide, 'edit');
 
511
        if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
 
512
            error("Failed to unhide the question.");
 
513
        }
 
514
        redirect($pageurl->out());
 
515
    }
 
516
}
 
517
 
 
518
/**
 
519
 * Shows the question bank editing interface.
 
520
 *
 
521
 * The function also processes a number of actions:
 
522
 *
 
523
 * Actions affecting the question pool:
 
524
 * move           Moves a question to a different category
 
525
 * deleteselected Deletes the selected questions from the category
 
526
 * Other actions:
 
527
 * category      Chooses the category
 
528
 * displayoptions Sets display options
 
529
 *
 
530
 * @author Martin Dougiamas and many others. This has recently been extensively
 
531
 *         rewritten by Gustav Delius and other members of the Serving Mathematics project
 
532
 *         {@link http://maths.york.ac.uk/serving_maths}
 
533
 * @param moodle_url $pageurl object representing this pages url.
 
534
 */
 
535
function question_showbank($tabname, $contexts, $pageurl, $cm, $page, $perpage, $sortorder, $sortorderdecoded, $cat, $recurse, $showhidden, $showquestiontext){
 
536
    global $COURSE;
 
537
 
 
538
    if (optional_param('deleteselected', false, PARAM_BOOL)){ // teacher still has to confirm
 
539
        // make a list of all the questions that are selected
 
540
        $rawquestions = $_REQUEST; // This code is called by both POST forms and GET links, so cannot use data_submitted.
 
541
        $questionlist = '';  // comma separated list of ids of questions to be deleted
 
542
        $questionnames = ''; // string with names of questions separated by <br /> with
 
543
                             // an asterix in front of those that are in use
 
544
        $inuse = false;      // set to true if at least one of the questions is in use
 
545
        foreach ($rawquestions as $key => $value) {    // Parse input for question ids
 
546
            if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
 
547
                $key = $matches[1];
 
548
                $questionlist .= $key.',';
 
549
                question_require_capability_on($key, 'edit');
 
550
                if (record_exists('quiz_question_instances', 'question', $key)) {
 
551
                    $questionnames .= '* ';
 
552
                    $inuse = true;
 
553
                }
 
554
                $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
 
555
            }
 
556
        }
 
557
        if (!$questionlist) { // no questions were selected
 
558
            redirect($pageurl->out());
 
559
        }
 
560
        $questionlist = rtrim($questionlist, ',');
 
561
 
 
562
        // Add an explanation about questions in use
 
563
        if ($inuse) {
 
564
            $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
 
565
        }
 
566
        notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
 
567
                    $pageurl->out_action(array('deleteselected'=>$questionlist, 'confirm'=>md5($questionlist))),
 
568
                    $pageurl->out_action());
 
569
 
 
570
        echo '</td></tr>';
 
571
        echo '</table>';
 
572
        print_footer($COURSE);
 
573
        exit;
 
574
    }
 
575
 
 
576
 
 
577
    // starts with category selection form
 
578
    print_box_start('generalbox questionbank');
 
579
    print_heading(get_string('questionbank', 'question'), '', 2);
 
580
    question_category_form($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, $recurse, $showhidden, $showquestiontext);
 
581
 
 
582
    // continues with list of questions
 
583
    question_list($contexts->having_one_edit_tab_cap($tabname), $pageurl, $cat, isset($cm) ? $cm : null,
 
584
            $recurse, $page, $perpage, $showhidden, $sortorder, $sortorderdecoded, $showquestiontext,
 
585
            $contexts->having_cap('moodle/question:add'));
 
586
 
 
587
    print_box_end();
 
588
}
 
589
/**
 
590
 * Common setup for all pages for editing questions.
 
591
 * @param string $edittab code for this edit tab
 
592
 * @param boolean $requirecmid require cmid? default false
 
593
 * @param boolean $requirecourseid require courseid, if cmid is not given? default true
 
594
 * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 
595
 */
 
596
function question_edit_setup($edittab, $requirecmid = false, $requirecourseid = true){
 
597
    global $COURSE, $QUESTION_EDITTABCAPS;
 
598
 
 
599
    //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array
 
600
    //of parameters that are passed from page to page.
 
601
    $thispageurl = new moodle_url();
 
602
    if ($requirecmid){
 
603
        $cmid =required_param('cmid', PARAM_INT);
 
604
    } else {
 
605
        $cmid = optional_param('cmid', 0, PARAM_INT);
 
606
    }
 
607
    if ($cmid){
 
608
        list($module, $cm) = get_module_from_cmid($cmid);
 
609
        $courseid = $cm->course;
 
610
        $thispageurl->params(compact('cmid'));
 
611
        require_login($courseid, false, $cm);
 
612
        $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
 
613
    } else {
 
614
        $module = null;
 
615
        $cm = null;
 
616
        if ($requirecourseid){
 
617
            $courseid  = required_param('courseid', PARAM_INT);
 
618
        } else {
 
619
            $courseid  = optional_param('courseid', 0, PARAM_INT);
 
620
        }
 
621
        if ($courseid){
 
622
            $thispageurl->params(compact('courseid'));
 
623
            require_login($courseid, false);
 
624
            $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
 
625
        } else {
 
626
            $thiscontext = null;
 
627
        }
 
628
    }
 
629
 
 
630
    if ($thiscontext){
 
631
        $contexts = new question_edit_contexts($thiscontext);
 
632
        $contexts->require_one_edit_tab_cap($edittab);
 
633
 
 
634
    } else {
 
635
        $contexts = null;
 
636
    }
 
637
 
 
638
 
 
639
 
 
640
    $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
 
641
 
 
642
    //pass 'cat' from page to page and when 'category' comes from a drop down menu
 
643
    //then we also reset the qpage so we go to page 1 of
 
644
    //a new cat.
 
645
    $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE);// if empty will be set up later
 
646
    if  ($category = optional_param('category', 0, PARAM_SEQUENCE)){
 
647
        if ($pagevars['cat'] != $category){ // is this a move to a new category?
 
648
            $pagevars['cat'] = $category;
 
649
            $pagevars['qpage'] = 0;
 
650
        }
 
651
    }
 
652
    if ($pagevars['cat']){
 
653
        $thispageurl->param('cat', $pagevars['cat']);
 
654
    }
 
655
    if ($pagevars['qpage'] > -1) {
 
656
        $thispageurl->param('qpage', $pagevars['qpage']);
 
657
    } else {
 
658
        $pagevars['qpage'] = 0;
 
659
    }
 
660
 
 
661
    $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT);
 
662
    if ($pagevars['qperpage'] > -1) {
 
663
        $thispageurl->param('qperpage', $pagevars['qperpage']);
 
664
    } else {
 
665
        $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE;
 
666
    }
 
667
 
 
668
    $sortoptions = array('alpha' => 'name, qtype ASC',
 
669
                          'typealpha' => 'qtype, name ASC',
 
670
                          'age' => 'id ASC');
 
671
 
 
672
    if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) {
 
673
        $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder];
 
674
        $pagevars['qsortorder'] = $sortorder;
 
675
        $thispageurl->param('qsortorder', $sortorder);
 
676
    } else {
 
677
        $pagevars['qsortorderdecoded'] = $sortoptions['typealpha'];
 
678
        $pagevars['qsortorder'] = 'typealpha';
 
679
    }
 
680
 
 
681
    $defaultcategory = question_make_default_categories($contexts->all());
 
682
 
 
683
    $contextlistarr = array();
 
684
    foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
 
685
        $contextlistarr[] = "'$context->id'";
 
686
    }
 
687
    $contextlist = join($contextlistarr, ' ,');
 
688
    if (!empty($pagevars['cat'])){
 
689
        $catparts = explode(',', $pagevars['cat']);
 
690
        if (!$catparts[0] || (FALSE !== array_search($catparts[1], $contextlistarr)) || !count_records_select("question_categories", "id = '".$catparts[0]."' AND contextid = $catparts[1]")) {
 
691
            print_error('invalidcategory', 'quiz');
 
692
        }
 
693
    } else {
 
694
        $category = $defaultcategory;
 
695
        $pagevars['cat'] = "$category->id,$category->contextid";
 
696
    }
 
697
 
 
698
    if(($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) {
 
699
        $pagevars['recurse'] = $recurse;
 
700
        $thispageurl->param('recurse', $recurse);
 
701
    } else {
 
702
        $pagevars['recurse'] = 1;
 
703
    }
 
704
 
 
705
    if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) {
 
706
        $pagevars['showhidden'] = $showhidden;
 
707
        $thispageurl->param('showhidden', $showhidden);
 
708
    } else {
 
709
        $pagevars['showhidden'] = 0;
 
710
    }
 
711
 
 
712
    if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) {
 
713
        $pagevars['showquestiontext'] = $showquestiontext;
 
714
        $thispageurl->param('showquestiontext', $showquestiontext);
 
715
    } else {
 
716
        $pagevars['showquestiontext'] = 0;
 
717
    }
 
718
 
 
719
    //category list page
 
720
    $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
 
721
    if ($pagevars['cpage'] < 1) {
 
722
        $pagevars['cpage'] = 1;
 
723
    }
 
724
    if ($pagevars['cpage'] != 1){
 
725
        $thispageurl->param('cpage', $pagevars['cpage']);
 
726
    }
 
727
 
 
728
 
 
729
    return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
 
730
}
 
731
class question_edit_contexts{
 
732
    var $allcontexts;
 
733
    /**
 
734
     * @param current context
 
735
     */
 
736
    function question_edit_contexts($thiscontext){
 
737
        $pcontextids = get_parent_contexts($thiscontext);
 
738
        $contexts = array($thiscontext);
 
739
        foreach ($pcontextids as $pcontextid){
 
740
            $contexts[] = get_context_instance_by_id($pcontextid);
 
741
        }
 
742
        $this->allcontexts = $contexts;
 
743
    }
 
744
    /**
 
745
     * @return array all parent contexts
 
746
     */
 
747
    function all(){
 
748
        return $this->allcontexts;
 
749
    }
 
750
    /**
 
751
     * @return object lowest context which must be either the module or course context
 
752
     */
 
753
    function lowest(){
 
754
        return $this->allcontexts[0];
 
755
    }
 
756
    /**
 
757
     * @param string $cap capability
 
758
     * @return array parent contexts having capability, zero based index
 
759
     */
 
760
    function having_cap($cap){
 
761
        $contextswithcap = array();
 
762
        foreach ($this->allcontexts as $context){
 
763
            if (has_capability($cap, $context)){
 
764
                $contextswithcap[] = $context;
 
765
            }
 
766
        }
 
767
        return $contextswithcap;
 
768
    }
 
769
    /**
 
770
     * @param array $caps capabilities
 
771
     * @return array parent contexts having at least one of $caps, zero based index
 
772
     */
 
773
    function having_one_cap($caps){
 
774
        $contextswithacap = array();
 
775
        foreach ($this->allcontexts as $context){
 
776
            foreach ($caps as $cap){
 
777
                if (has_capability($cap, $context)){
 
778
                    $contextswithacap[] = $context;
 
779
                    break; //done with caps loop
 
780
                }
 
781
            }
 
782
        }
 
783
        return $contextswithacap;
 
784
    }
 
785
    /**
 
786
     * @param string $tabname edit tab name
 
787
     * @return array parent contexts having at least one of $caps, zero based index
 
788
     */
 
789
    function having_one_edit_tab_cap($tabname){
 
790
        global $QUESTION_EDITTABCAPS;
 
791
        return $this->having_one_cap($QUESTION_EDITTABCAPS[$tabname]);
 
792
    }
 
793
    /**
 
794
     * Has at least one parent context got the cap $cap?
 
795
     *
 
796
     * @param string $cap capability
 
797
     * @return boolean
 
798
     */
 
799
    function have_cap($cap){
 
800
        return (count($this->having_cap($cap)));
 
801
    }
 
802
 
 
803
    /**
 
804
     * Has at least one parent context got one of the caps $caps?
 
805
     *
 
806
     * @param string $cap capability
 
807
     * @return boolean
 
808
     */
 
809
    function have_one_cap($caps){
 
810
        foreach ($caps as $cap){
 
811
            if ($this->have_cap($cap)){
 
812
                return true;
 
813
            }
 
814
        }
 
815
        return false;
 
816
    }
 
817
    /**
 
818
     * Has at least one parent context got one of the caps for actions on $tabname
 
819
     *
 
820
     * @param string $tabname edit tab name
 
821
     * @return boolean
 
822
     */
 
823
    function have_one_edit_tab_cap($tabname){
 
824
        global $QUESTION_EDITTABCAPS;
 
825
        return $this->have_one_cap($QUESTION_EDITTABCAPS[$tabname]);
 
826
    }
 
827
    /**
 
828
     * Throw error if at least one parent context hasn't got the cap $cap
 
829
     *
 
830
     * @param string $cap capability
 
831
     */
 
832
    function require_cap($cap){
 
833
        if (!$this->have_cap($cap)){
 
834
            print_error('nopermissions', '', '', $cap);
 
835
        }
 
836
    }
 
837
    /**
 
838
     * Throw error if at least one parent context hasn't got one of the caps $caps
 
839
     *
 
840
     * @param array $cap capabilities
 
841
     */
 
842
     function require_one_cap($caps){
 
843
        if (!$this->have_one_cap($caps)){
 
844
            $capsstring = join($caps, ', ');
 
845
            print_error('nopermissions', '', '', $capsstring);
 
846
        }
 
847
    }
 
848
    /**
 
849
     * Throw error if at least one parent context hasn't got one of the caps $caps
 
850
     *
 
851
     * @param string $tabname edit tab name
 
852
     */
 
853
     function require_one_edit_tab_cap($tabname){
 
854
        if (!$this->have_one_edit_tab_cap($tabname)){
 
855
            print_error('nopermissions', '', '', 'access question edit tab '.$tabname);
 
856
        }
 
857
    }
 
858
}
 
859
 
 
860
//capabilities for each page of edit tab.
 
861
//this determines which contexts' categories are available. At least one
 
862
//page is displayed if user has one of the capability on at least one context
 
863
$QUESTION_EDITTABCAPS = array(
 
864
                            'editq' => array('moodle/question:add',
 
865
                                'moodle/question:editmine',
 
866
                                'moodle/question:editall',
 
867
                                'moodle/question:viewmine',
 
868
                                'moodle/question:viewall',
 
869
                                'moodle/question:usemine',
 
870
                                'moodle/question:useall',
 
871
                                'moodle/question:movemine',
 
872
                                'moodle/question:moveall'),
 
873
                            'questions'=>array('moodle/question:add',
 
874
                                'moodle/question:editmine',
 
875
                                'moodle/question:editall',
 
876
                                'moodle/question:viewmine',
 
877
                                'moodle/question:viewall',
 
878
                                'moodle/question:movemine',
 
879
                                'moodle/question:moveall'),
 
880
                           'categories'=>array('moodle/question:managecategory'),
 
881
                           'import'=>array('moodle/question:add'),
 
882
                           'export'=>array('moodle/question:viewall', 'moodle/question:viewmine'));
 
883
 
 
884
 
 
885
 
 
886
/**
 
887
 * Make sure user is logged in as required in this context.
 
888
 */
 
889
function require_login_in_context($contextorid = null){
 
890
    if (!is_object($contextorid)){
 
891
        $context = get_context_instance_by_id($contextorid);
 
892
    } else {
 
893
        $context = $contextorid;
 
894
    }
 
895
    if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
 
896
        require_login($context->instanceid);
 
897
    } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
 
898
        if ($cm = get_record('course_modules','id',$context->instanceid)) {
 
899
            if (!$course = get_record('course', 'id', $cm->course)) {
 
900
                error('Incorrect course.');
 
901
            }
 
902
            require_course_login($course, true, $cm);
 
903
 
 
904
        } else {
 
905
            error('Incorrect course module id.');
 
906
        }
 
907
    } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
 
908
        if (!empty($CFG->forcelogin)) {
 
909
            require_login();
 
910
        }
 
911
 
 
912
    } else {
 
913
        require_login();
 
914
    }
 
915
}
 
916
?>