forked from kanemura1206/maspen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editlib.php
1904 lines (1673 loc) · 67.5 KB
/
editlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Functions used to show question editing interface
*
* @package moodlecore
* @subpackage questionbank
* @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/questionlib.php');
define('DEFAULT_QUESTIONS_PER_PAGE', 20);
function get_module_from_cmid($cmid) {
global $CFG, $DB;
if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
FROM {course_modules} cm,
{modules} md
WHERE cm.id = ? AND
md.id = cm.module", array($cmid))){
print_error('invalidcoursemodule');
} elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
print_error('invalidcoursemodule');
}
$modrec->instance = $modrec->id;
$modrec->cmid = $cmrec->id;
$cmrec->name = $modrec->name;
return array($modrec, $cmrec);
}
/**
* Function to read all questions for category into big array
*
* @param int $category category number
* @param bool $noparent if true only questions with NO parent will be selected
* @param bool $recurse include subdirectories
* @param bool $export set true if this is called by questionbank export
*/
function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
global $DB;
// Build sql bit for $noparent
$npsql = '';
if ($noparent) {
$npsql = " and parent='0' ";
}
// Get list of categories
if ($recurse) {
$categorylist = question_categorylist($category->id);
} else {
$categorylist = array($category->id);
}
// Get the list of questions for the category
list($usql, $params) = $DB->get_in_or_equal($categorylist);
$questions = $DB->get_records_select('question', "category $usql $npsql", $params, 'qtype, name');
// Iterate through questions, getting stuff we need
$qresults = array();
foreach($questions as $key => $question) {
$question->export_process = $export;
$qtype = question_bank::get_qtype($question->qtype, false);
if ($export && $qtype->name() == 'missingtype') {
// Unrecognised question type. Skip this question when exporting.
continue;
}
$qtype->get_question_options($question);
$qresults[] = $question;
}
return $qresults;
}
/**
* @param int $categoryid a category id.
* @return bool whether this is the only top-level category in a context.
*/
function question_is_only_toplevel_category_in_context($categoryid) {
global $DB;
return 1 == $DB->count_records_sql("
SELECT count(*)
FROM {question_categories} c1,
{question_categories} c2
WHERE c2.id = ?
AND c1.contextid = c2.contextid
AND c1.parent = 0 AND c2.parent = 0", array($categoryid));
}
/**
* Check whether this user is allowed to delete this category.
*
* @param int $todelete a category id.
*/
function question_can_delete_cat($todelete) {
global $DB;
if (question_is_only_toplevel_category_in_context($todelete)) {
print_error('cannotdeletecate', 'question');
} else {
$contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
}
}
/**
* Base class for representing a column in a {@link question_bank_view}.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class question_bank_column_base {
/**
* @var question_bank_view
*/
protected $qbank;
/** @var bool determine whether the column is td or th. */
protected $isheading = false;
/**
* Constructor.
* @param $qbank the question_bank_view we are helping to render.
*/
public function __construct(question_bank_view $qbank) {
$this->qbank = $qbank;
$this->init();
}
/**
* A chance for subclasses to initialise themselves, for example to load lang strings,
* without having to override the constructor.
*/
protected function init() {
}
/**
* Set the column as heading
*/
public function set_as_heading() {
$this->isheading = true;
}
public function is_extra_row() {
return false;
}
/**
* Output the column header cell.
*/
public function display_header() {
echo '<th class="header ' . $this->get_classes() . '" scope="col">';
$sortable = $this->is_sortable();
$name = $this->get_name();
$title = $this->get_title();
$tip = $this->get_title_tip();
if (is_array($sortable)) {
if ($title) {
echo '<div class="title">' . $title . '</div>';
}
$links = array();
foreach ($sortable as $subsort => $details) {
$links[] = $this->make_sort_link($name . '_' . $subsort,
$details['title'], '', !empty($details['reverse']));
}
echo '<div class="sorters">' . implode(' / ', $links) . '</div>';
} else if ($sortable) {
echo $this->make_sort_link($name, $title, $tip);
} else {
if ($tip) {
echo '<span title="' . $tip . '">';
}
echo $title;
if ($tip) {
echo '</span>';
}
}
echo "</th>\n";
}
/**
* Title for this column. Not used if is_sortable returns an array.
* @param object $question the row from the $question table, augmented with extra information.
* @param string $rowclasses CSS class names that should be applied to this row of output.
*/
protected abstract function get_title();
/**
* @return string a fuller version of the name. Use this when get_title() returns
* something very short, and you want a longer version as a tool tip.
*/
protected function get_title_tip() {
return '';
}
/**
* Get a link that changes the sort order, and indicates the current sort state.
* @param $name internal name used for this type of sorting.
* @param $currentsort the current sort order -1, 0, 1 for descending, none, ascending.
* @param $title the link text.
* @param $defaultreverse whether the default sort order for this column is descending, rather than ascending.
* @return string HTML fragment.
*/
protected function make_sort_link($sort, $title, $tip, $defaultreverse = false) {
$currentsort = $this->qbank->get_primary_sort_order($sort);
$newsortreverse = $defaultreverse;
if ($currentsort) {
$newsortreverse = $currentsort > 0;
}
if (!$tip) {
$tip = $title;
}
if ($newsortreverse) {
$tip = get_string('sortbyxreverse', '', $tip);
} else {
$tip = get_string('sortbyx', '', $tip);
}
$link = '<a href="' . $this->qbank->new_sort_url($sort, $newsortreverse) . '" title="' . $tip . '">';
$link .= $title;
if ($currentsort) {
$link .= $this->get_sort_icon($currentsort < 0);
}
$link .= '</a>';
return $link;
}
/**
* Get an icon representing the corrent sort state.
* @param $reverse sort is descending, not ascending.
* @return string HTML image tag.
*/
protected function get_sort_icon($reverse) {
global $OUTPUT;
if ($reverse) {
return $OUTPUT->pix_icon('t/sort_desc', get_string('desc'), '', array('class' => 'iconsort'));
} else {
return $OUTPUT->pix_icon('t/sort_asc', get_string('asc'), '', array('class' => 'iconsort'));
}
}
/**
* Output this column.
* @param object $question the row from the $question table, augmented with extra information.
* @param string $rowclasses CSS class names that should be applied to this row of output.
*/
public function display($question, $rowclasses) {
$this->display_start($question, $rowclasses);
$this->display_content($question, $rowclasses);
$this->display_end($question, $rowclasses);
}
/**
* Output the opening column tag. If it is set as heading, it will use <th> tag instead of <td>
*
* @param stdClass $question
* @param array $rowclasses
*/
protected function display_start($question, $rowclasses) {
$tag = 'td';
$attr = array('class' => $this->get_classes());
if ($this->isheading) {
$tag = 'th';
$attr['scope'] = 'row';
}
echo html_writer::start_tag($tag, $attr);
}
/**
* @return string the CSS classes to apply to every cell in this column.
*/
protected function get_classes() {
$classes = $this->get_extra_classes();
$classes[] = $this->get_name();
return implode(' ', $classes);
}
/**
* @param object $question the row from the $question table, augmented with extra information.
* @return string internal name for this column. Used as a CSS class name,
* and to store information about the current sort. Must match PARAM_ALPHA.
*/
public abstract function get_name();
/**
* @return array any extra class names you would like applied to every cell in this column.
*/
public function get_extra_classes() {
return array();
}
/**
* Output the contents of this column.
* @param object $question the row from the $question table, augmented with extra information.
* @param string $rowclasses CSS class names that should be applied to this row of output.
*/
protected abstract function display_content($question, $rowclasses);
/**
* Output the closing column tag
*
* @param object $question
* @param string $rowclasses
*/
protected function display_end($question, $rowclasses) {
$tag = 'td';
if ($this->isheading) {
$tag = 'th';
}
echo html_writer::end_tag($tag);
}
/**
* Return an array 'table_alias' => 'JOIN clause' to bring in any data that
* this column required.
*
* The return values for all the columns will be checked. It is OK if two
* columns join in the same table with the same alias and identical JOIN clauses.
* If to columns try to use the same alias with different joins, you get an error.
* The only table included by default is the question table, which is aliased to 'q'.
*
* It is importnat that your join simply adds additional data (or NULLs) to the
* existing rows of the query. It must not cause additional rows.
*
* @return array 'table_alias' => 'JOIN clause'
*/
public function get_extra_joins() {
return array();
}
/**
* @return array fields required. use table alias 'q' for the question table, or one of the
* ones from get_extra_joins. Every field requested must specify a table prefix.
*/
public function get_required_fields() {
return array();
}
/**
* Can this column be sorted on? You can return either:
* + false for no (the default),
* + a field name, if sorting this column corresponds to sorting on that datbase field.
* + an array of subnames to sort on as follows
* return array(
* 'firstname' => array('field' => 'uc.firstname', 'title' => get_string('firstname')),
* 'lastname' => array('field' => 'uc.lastname', 'field' => get_string('lastname')),
* );
* As well as field, and field, you can also add 'revers' => 1 if you want the default sort
* order to be DESC.
* @return mixed as above.
*/
public function is_sortable() {
return false;
}
/**
* Helper method for building sort clauses.
* @param bool $reverse whether the normal direction should be reversed.
* @param string $normaldir 'ASC' or 'DESC'
* @return string 'ASC' or 'DESC'
*/
protected function sortorder($reverse) {
if ($reverse) {
return ' DESC';
} else {
return ' ASC';
}
}
/**
* @param $reverse Whether to sort in the reverse of the default sort order.
* @param $subsort if is_sortable returns an array of subnames, then this will be
* one of those. Otherwise will be empty.
* @return string some SQL to go in the order by clause.
*/
public function sort_expression($reverse, $subsort) {
$sortable = $this->is_sortable();
if (is_array($sortable)) {
if (array_key_exists($subsort, $sortable)) {
return $sortable[$subsort]['field'] . $this->sortorder($reverse, !empty($sortable[$subsort]['reverse']));
} else {
throw new coding_exception('Unexpected $subsort type: ' . $subsort);
}
} else if ($sortable) {
return $sortable . $this->sortorder($reverse);
} else {
throw new coding_exception('sort_expression called on a non-sortable column.');
}
}
}
/**
* A column with a checkbox for each question with name q{questionid}.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_checkbox_column extends question_bank_column_base {
protected $strselect;
protected $firstrow = true;
public function init() {
$this->strselect = get_string('select');
}
public function get_name() {
return 'checkbox';
}
protected function get_title() {
return '<input type="checkbox" disabled="disabled" id="qbheadercheckbox" />';
}
protected function get_title_tip() {
return get_string('selectquestionsforbulk', 'question');
}
protected function display_content($question, $rowclasses) {
global $PAGE;
echo '<input title="' . $this->strselect . '" type="checkbox" name="q' .
$question->id . '" id="checkq' . $question->id . '" value="1"/>';
if ($this->firstrow) {
$PAGE->requires->js('/question/qengine.js');
$module = array(
'name' => 'qbank',
'fullpath' => '/question/qbank.js',
'requires' => array('yui2-dom', 'yui2-event', 'yui2-container'),
'strings' => array(),
'async' => false,
);
$PAGE->requires->js_init_call('question_bank.init_checkbox_column', array(get_string('selectall'),
get_string('deselectall'), 'checkq' . $question->id), false, $module);
$this->firstrow = false;
}
}
public function get_required_fields() {
return array('q.id');
}
}
/**
* A column type for the name of the question type.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_question_type_column extends question_bank_column_base {
public function get_name() {
return 'qtype';
}
protected function get_title() {
return get_string('qtypeveryshort', 'question');
}
protected function get_title_tip() {
return get_string('questiontype', 'question');
}
protected function display_content($question, $rowclasses) {
echo print_question_icon($question);
}
public function get_required_fields() {
return array('q.qtype');
}
public function is_sortable() {
return 'q.qtype';
}
}
/**
* A column type for the name of the question name.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_question_name_column extends question_bank_column_base {
protected $checkboxespresent = null;
public function get_name() {
return 'questionname';
}
protected function get_title() {
return get_string('question');
}
protected function label_for($question) {
if (is_null($this->checkboxespresent)) {
$this->checkboxespresent = $this->qbank->has_column('checkbox');
}
if ($this->checkboxespresent) {
return 'checkq' . $question->id;
} else {
return '';
}
}
protected function display_content($question, $rowclasses) {
$labelfor = $this->label_for($question);
if ($labelfor) {
echo '<label for="' . $labelfor . '">';
}
echo format_string($question->name);
if ($labelfor) {
echo '</label>';
}
}
public function get_required_fields() {
return array('q.id', 'q.name');
}
public function is_sortable() {
return 'q.name';
}
}
/**
* A column type for the name of the question creator.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_creator_name_column extends question_bank_column_base {
public function get_name() {
return 'creatorname';
}
protected function get_title() {
return get_string('createdby', 'question');
}
protected function display_content($question, $rowclasses) {
if (!empty($question->creatorfirstname) && !empty($question->creatorlastname)) {
$u = new stdClass();
$u->firstname = $question->creatorfirstname;
$u->lastname = $question->creatorlastname;
echo fullname($u);
}
}
public function get_extra_joins() {
return array('uc' => 'LEFT JOIN {user} uc ON uc.id = q.createdby');
}
public function get_required_fields() {
return array('uc.firstname AS creatorfirstname', 'uc.lastname AS creatorlastname');
}
public function is_sortable() {
return array(
'firstname' => array('field' => 'uc.firstname', 'title' => get_string('firstname')),
'lastname' => array('field' => 'uc.lastname', 'title' => get_string('lastname')),
);
}
}
/**
* A column type for the name of the question last modifier.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_modifier_name_column extends question_bank_column_base {
public function get_name() {
return 'modifiername';
}
protected function get_title() {
return get_string('lastmodifiedby', 'question');
}
protected function display_content($question, $rowclasses) {
if (!empty($question->modifierfirstname) && !empty($question->modifierlastname)) {
$u = new stdClass();
$u->firstname = $question->modifierfirstname;
$u->lastname = $question->modifierlastname;
echo fullname($u);
}
}
public function get_extra_joins() {
return array('um' => 'LEFT JOIN {user} um ON um.id = q.modifiedby');
}
public function get_required_fields() {
return array('um.firstname AS modifierfirstname', 'um.lastname AS modifierlastname');
}
public function is_sortable() {
return array(
'firstname' => array('field' => 'um.firstname', 'title' => get_string('firstname')),
'lastname' => array('field' => 'um.lastname', 'title' => get_string('lastname')),
);
}
}
/**
* A base class for actions that are an icon that lets you manipulate the question in some way.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class question_bank_action_column_base extends question_bank_column_base {
protected function get_title() {
return ' ';
}
public function get_extra_classes() {
return array('iconcol');
}
protected function print_icon($icon, $title, $url) {
global $OUTPUT;
echo '<a title="' . $title . '" href="' . $url . '">
<img src="' . $OUTPUT->pix_url($icon) . '" class="iconsmall" alt="' . $title . '" /></a>';
}
public function get_required_fields() {
// createdby is required for permission checks.
return array('q.id', 'q.createdby');
}
}
/**
* Base class for question bank columns that just contain an action icon.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_edit_action_column extends question_bank_action_column_base {
protected $stredit;
protected $strview;
public function init() {
parent::init();
$this->stredit = get_string('edit');
$this->strview = get_string('view');
}
public function get_name() {
return 'editaction';
}
protected function display_content($question, $rowclasses) {
if (question_has_capability_on($question, 'edit')) {
$this->print_icon('t/edit', $this->stredit, $this->qbank->edit_question_url($question->id));
} else if (question_has_capability_on($question, 'view')) {
$this->print_icon('i/info', $this->strview, $this->qbank->edit_question_url($question->id));
}
}
}
/**
* Question bank columns for the preview action icon.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_preview_action_column extends question_bank_action_column_base {
protected $strpreview;
public function init() {
parent::init();
$this->strpreview = get_string('preview');
}
public function get_name() {
return 'previewaction';
}
protected function display_content($question, $rowclasses) {
global $OUTPUT;
if (question_has_capability_on($question, 'use')) {
// Build the icon.
$image = $OUTPUT->pix_icon('t/preview', $this->strpreview);
$link = $this->qbank->preview_question_url($question);
$action = new popup_action('click', $link, 'questionpreview',
question_preview_popup_params());
echo $OUTPUT->action_link($link, $image, $action, array('title' => $this->strpreview));
}
}
public function get_required_fields() {
return array('q.id');
}
}
/**
* Question bank columns for the move action icon.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_move_action_column extends question_bank_action_column_base {
protected $strmove;
public function init() {
parent::init();
$this->strmove = get_string('move');
}
public function get_name() {
return 'moveaction';
}
protected function display_content($question, $rowclasses) {
if (question_has_capability_on($question, 'move')) {
$this->print_icon('t/move', $this->strmove, $this->qbank->move_question_url($question->id));
}
}
}
/**
* action to delete (or hide) a question, or restore a previously hidden question.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_delete_action_column extends question_bank_action_column_base {
protected $strdelete;
protected $strrestore;
public function init() {
parent::init();
$this->strdelete = get_string('delete');
$this->strrestore = get_string('restore');
}
public function get_name() {
return 'deleteaction';
}
protected function display_content($question, $rowclasses) {
if (question_has_capability_on($question, 'edit')) {
if ($question->hidden) {
$url = new moodle_url($this->qbank->base_url(), array('unhide' => $question->id, 'sesskey'=>sesskey()));
$this->print_icon('t/restore', $this->strrestore, $url);
} else {
$url = new moodle_url($this->qbank->base_url(), array('deleteselected' => $question->id, 'q' . $question->id => 1, 'sesskey'=>sesskey()));
$this->print_icon('t/delete', $this->strdelete, $url);
}
}
}
public function get_required_fields() {
return array('q.id', 'q.hidden');
}
}
/**
* Base class for 'columns' that are actually displayed as a row following the main question row.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class question_bank_row_base extends question_bank_column_base {
public function is_extra_row() {
return true;
}
protected function display_start($question, $rowclasses) {
if ($rowclasses) {
echo '<tr class="' . $rowclasses . '">' . "\n";
} else {
echo "<tr>\n";
}
echo '<td colspan="' . $this->qbank->get_column_count() . '" class="' . $this->get_name() . '">';
}
protected function display_end($question, $rowclasses) {
echo "</td></tr>\n";
}
}
/**
* A column type for the name of the question name.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_question_text_row extends question_bank_row_base {
protected $formatoptions;
protected function init() {
$this->formatoptions = new stdClass();
$this->formatoptions->noclean = true;
$this->formatoptions->para = false;
}
public function get_name() {
return 'questiontext';
}
protected function get_title() {
return get_string('questiontext', 'question');
}
protected function display_content($question, $rowclasses) {
$text = question_rewrite_questiontext_preview_urls($question->questiontext,
$question->contextid, 'question', $question->id);
$text = format_text($text, $question->questiontextformat,
$this->formatoptions);
if ($text == '') {
$text = ' ';
}
echo $text;
}
public function get_extra_joins() {
return array('qc' => 'JOIN {question_categories} qc ON qc.id = q.category');
}
public function get_required_fields() {
return array('q.id', 'q.questiontext', 'q.questiontextformat', 'qc.contextid');
}
}
/**
* This class prints a view of the question bank, including
* + Some controls to allow users to to select what is displayed.
* + A list of questions as a table.
* + Further controls to do things with the questions.
*
* This class gives a basic view, and provides plenty of hooks where subclasses
* can override parts of the display.
*
* The list of questions presented as a table is generated by creating a list of
* question_bank_column objects, one for each 'column' to be displayed. These
* manage
* + outputting the contents of that column, given a $question object, but also
* + generating the right fragments of SQL to ensure the necessary data is present,
* and sorted in the right order.
* + outputting table headers.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_bank_view {
const MAX_SORTS = 3;
protected $baseurl;
protected $editquestionurl;
protected $quizorcourseid;
protected $contexts;
protected $cm;
protected $course;
protected $knowncolumntypes;
protected $visiblecolumns;
protected $extrarows;
protected $requiredcolumns;
protected $sort;
protected $lastchangedid;
protected $countsql;
protected $loadsql;
protected $sqlparams;
/**
* Constructor
* @param question_edit_contexts $contexts
* @param moodle_url $pageurl
* @param object $course course settings
* @param object $cm (optional) activity settings.
*/
public function __construct($contexts, $pageurl, $course, $cm = null) {
global $CFG, $PAGE;
$this->contexts = $contexts;
$this->baseurl = $pageurl;
$this->course = $course;
$this->cm = $cm;
if (!empty($cm) && $cm->modname == 'quiz') {
$this->quizorcourseid = '&quizid=' . $cm->instance;
} else {
$this->quizorcourseid = '&courseid=' .$this->course->id;
}
// Create the url of the new question page to forward to.
$returnurl = $pageurl->out_as_local_url(false);
$this->editquestionurl = new moodle_url('/question/question.php',
array('returnurl' => $returnurl));
if ($cm !== null){
$this->editquestionurl->param('cmid', $cm->id);
} else {
$this->editquestionurl->param('courseid', $this->course->id);
}
$this->lastchangedid = optional_param('lastchanged',0,PARAM_INT);
$this->init_column_types();
$this->init_columns($this->wanted_columns(), $this->heading_column());
$this->init_sort();
}
protected function wanted_columns() {
$columns = array('checkbox', 'qtype', 'questionname', 'editaction',
'previewaction', 'moveaction', 'deleteaction', 'creatorname',
'modifiername');
if (optional_param('qbshowtext', false, PARAM_BOOL)) {
$columns[] = 'questiontext';
}
return $columns;
}
/**
* Specify the column heading
*
* @return string Column name for the heading
*/
protected function heading_column() {
return 'questionname';
}
protected function known_field_types() {
return array(
new question_bank_checkbox_column($this),
new question_bank_question_type_column($this),
new question_bank_question_name_column($this),
new question_bank_creator_name_column($this),
new question_bank_modifier_name_column($this),
new question_bank_edit_action_column($this),
new question_bank_preview_action_column($this),
new question_bank_move_action_column($this),
new question_bank_delete_action_column($this),
new question_bank_question_text_row($this),
);
}
protected function init_column_types() {
$this->knowncolumntypes = array();
foreach ($this->known_field_types() as $col) {
$this->knowncolumntypes[$col->get_name()] = $col;
}
}
/**
* Initializing table columns
*
* @param array $wanted Collection of column names
* @param string $heading The name of column that is set as heading
*/
protected function init_columns($wanted, $heading = '') {
$this->visiblecolumns = array();
$this->extrarows = array();
foreach ($wanted as $colname) {
if (!isset($this->knowncolumntypes[$colname])) {
throw new coding_exception('Unknown column type ' . $colname . ' requested in init columns.');
}
$column = $this->knowncolumntypes[$colname];
if ($column->is_extra_row()) {
$this->extrarows[$colname] = $column;
} else {
$this->visiblecolumns[$colname] = $column;
}
}
$this->requiredcolumns = array_merge($this->visiblecolumns, $this->extrarows);
if (array_key_exists($heading, $this->requiredcolumns)) {
$this->requiredcolumns[$heading]->set_as_heading();
}
}
/**
* @param string $colname a column internal name.
* @return bool is this column included in the output?