-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse.module
3101 lines (2794 loc) · 97.2 KB
/
course.module
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
/**
* @file course.module
* Core functionality for Courses.
*/
// Course outline functions
require_once 'includes/course.outline.inc';
// Course exporting functions
require_once 'includes/course.exporting.inc';
// Rules support
require_once 'includes/course.rules.inc';
/**
* Implements hook_menu().
*/
function course_menu() {
$items = array();
// Base configuration.
$items['admin/course'] = array(
'title' => 'Course',
'description' => 'Configure courses.',
'page callback' => 'course_settings_overview',
'access callback' => 'course_admin_access',
'file' => 'includes/course.settings.inc',
);
// Default tab for settings.
$items['admin/course/overview'] = array(
'title' => 'Overview',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['course/autocomplete/node/%'] = array(
'page callback' => 'course_object_autocomplete_node',
'access arguments' => array('access content'),
'page arguments' => array(3, 4),
);
// Course settings handler forms. This gives organization and consistency to
// form placement for each module that defines settings handlers through
// hook_course_handlers().
$modules = course_get_handlers('settings');
$default_set = array();
$handlers = array();
$packages = array();
// Flatten each module's settings handlers into one array, so we can get info
// about any other handler-package while looping over each handler below.
foreach ($modules as $module_key => $settings) {
if (is_array($settings)) {
// Define
foreach ($settings as $handler_key => $handler_info) {
// Manually set the implementing module key. It would be unnecessary to
// force implementing modules to set this, since we can get it here.
$handler_info['module'] = $module_key;
// Manually set which package the handler belongs in. If one is not
// defined, assume the handler is it's own package.
$handler_info['package'] = isset($handler_info['package']) ? $handler_info['package'] : $handler_key;
// Build the array of handlers. Add this handler with a combined key,
// so module defined settings handlers can avoid namespace conflicts.
$module_handler_key = "{$module_key}_{$handler_key}";
$handlers[$module_handler_key] = $handler_info;
// Build a reverse array of handler keys - keyed by package - so we can
// get package info below when we need it. If there are duplicate
// handler/package keys, use the first one for grouping the others.
$package_key = $handler_info['package'] ? $handler_info['package'] : $handler_key;
if (!isset($packages[$package_key])) {
$packages[$package_key] = $module_handler_key;
}
}
}
}
// Loop over each handler, and set tabs accordingly.
foreach ($handlers as $module_handler_key => $handler_info) {
// Get package info for this handler.
$package_key = $handler_info['package'];
$package_info = $handlers[$packages[$package_key]];
// Define a path for the handler's specified package.
$package_router = "admin/course/{$package_key}";
// Define a path for the handler.
$handler_router = "admin/course/{$package_key}/{$module_handler_key}";
// Add the handler item, either as the default page content
// (MENU_NORMAL_ITEM will work with the MENU_DEFAULT_LOCAL_TASK below)
// or as one of the other MENU_LOCAL_TASK tabs). If this is the deafult
// page content, the router path and title will be taken from the
// handler which defined the package.
$item_router = !isset($default_set[$package_key]) ? $package_router : $handler_router;
$item_title = !isset($default_set[$package_key]) ? $package_info['name'] : $handler_info['name'];
$item_type = MENU_NORMAL_ITEM;
$items[$item_router] = array(
'title' => $item_title,
'description' => $handler_info['description'],
'access arguments' => array('administer course'),
'page callback' => 'drupal_get_form',
'page arguments' => array($handler_info['callback']),
'type' => MENU_NORMAL_ITEM,
);
// Append file info to $items, if specified.
$file_info = array();
if (isset($handler_info['file'])) {
// Define the item 'file' key.
$items[$item_router]['file'] = $handler_info['file'];
// Define the item 'file path' key.
if (isset($handler_info['file path'])) {
// Use the path if provided. If not provided, we need to specify the
// handler provider module path, otherwise hook_menu() assumes
// 'file path' is the path to it's implementing module (Course).
$items[$item_router]['file path'] = $handler_info['file path'] ? $handler_info['file path'] : drupal_get_path('module', $handler_info['module']);
}
}
// Check if a default tab has already been set for this module.
if (!isset($default_set[$package_key])) {
// Add the default tab with the handler router item and name. We do
// this here so the first handler settings form always displays as the
// default page content at the module router item path.
$items[$handler_router] = array(
'title' => t('Settings'),
'type' => MENU_NORMAL_ITEM,
);
// Flag MENU_DEFAULT_LOCAL_TASK as set for this module.
$default_set[$package_key] = TRUE;
}
}
// Landing page for course completion.
$items['node/%course/course-outline'] = array(
'title' => 'Course outline',
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'page arguments' => array('course_outline_overview_form'),
'page callback' => 'drupal_get_form',
'type' => MENU_LOCAL_TASK,
'file' => 'includes/course.outline.inc',
);
// Landing page for course completion.
$items['node/%course/course-complete'] = array(
'title' => 'Course complete',
'access arguments' => array(1),
'access callback' => 'course_completion_page_access',
'page arguments' => array(1),
'page callback' => 'course_completion_page',
'file' => 'includes/course.outline.inc',
);
// Display the 'Take course' menu item as a tab or link, depending.
$items['node/%course/takecourse'] = array(
'title' => 'Take course',
'title callback' => 'course_takecourse_title',
'title arguments' => array(1),
'description' => 'Take course.',
'page callback' => 'course_take_course',
'page arguments' => array(1),
'access callback' => 'course_take_course_menu_access',
'access arguments' => array(1),
'type' => variable_get('course_takecourse_tab_display', 1) ? MENU_LOCAL_TASK : MENU_CALLBACK,
);
// Reports page listing each course object.
$items['node/%course/course-reports/objects'] = array(
'title' => 'Course objects',
'type' => MENU_LOCAL_TASK,
'page callback' => 'course_object_reports_page',
'page arguments' => array(1),
'access callback' => '_course_reports_access',
'access arguments' => array(1),
'file' => 'includes/course.reports.inc',
);
// Global report area
$items['admin/reports/course'] = array(
'title' => 'Course reports',
'description' => 'View and download course information.',
'access arguments' => array('access all course reports'),
'page callback' => 'system_admin_menu_block_page',
'file path' => drupal_get_path('module', 'system'),
'file' => 'system.admin.inc',
);
// Course object
$items['node/%course/course-object/%course_object'] = array(
'title' => 'Course object router',
'page callback' => 'course_object_take',
'page arguments' => array(3),
'access callback' => 'course_access_object',
'access arguments' => array(1, 3),
'weight' => 2,
);
// Course object edit
$items['node/%course/course-object/%ctools_js/%course_object/options'] = array(
'title' => 'Course object settings',
'page callback' => 'course_object_options',
'page arguments' => array(1, 3, 4),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
);
// Course object edit
$items['node/%course/course-object/%ctools_js/%course_object/restore'] = array(
'page callback' => 'course_object_restore',
'page arguments' => array(1, 3, 4),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'type' => MENU_CALLBACK,
);
// AHAH handler.
$items['node/%course/course-outline/%ctools_js/more/%'] = array(
'page callback' => 'course_outline_overview_js_more',
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'type' => MENU_CALLBACK,
'page arguments' => array(1, 3, 5),
);
$items['node/%course/course-object/%course_object/%ctools_js/nav'] = array(
'page callback' => 'course_ajaj_fulfullment_check',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'page arguments' => array(1, 3, 5),
);
if (module_exists('devel_generate')) {
$items['admin/config/development/generate/course'] = array(
'title' => 'Generate course',
'description' => 'Generate a given number of courses and object.',
'access arguments' => array('administer course'),
'page callback' => 'drupal_get_form',
'page arguments' => array('course_generate_form'),
'file' => 'course.devel.inc',
);
}
return $items;
}
/**
* Implements hook_course_handlers().
*
* @see course_menu()
* @see course_settings_overview()
*/
function course_course_handlers() {
$outline = 'includes/course.outline.inc';
$settings = 'includes/course.settings.inc';
return array(
'outline' => array(
'course' => array(
'name' => t('Course'),
'description' => t('Stock outline display.'),
'callback' => 'course_outline_list',
'file' => $outline,
),
'none' => array(
'name' => t('None'),
'description' => t('No outline provided (placeholder course).'),
),
),
'context' => array(
'node' => array(
'callback' => 'course_context',
),
),
'settings' => array(
'appearance' => array(
'name' => t('Appearance'),
'description' => t('Configure the course appearance, including outline style, disabling regions, and <em>enroll</em> and <em>take course</em> links.'),
'callback' => 'course_settings_appearance_form',
'file' => $settings,
),
'enrollment' => array(
'name' => t('Enrollments'),
'description' => t('Configure enrollments.'),
'callback' => 'course_enrollment_settings_form',
'file' => $settings,
),
'report' => array(
'name' => t('Reports'),
'description' => t('Configure course reporting.'),
'callback' => 'course_report_settings_form',
'file' => $settings,
),
'object' => array(
'name' => t('Objects'),
'description' => t('Configure course objects.'),
'callback' => 'course_object_settings_form',
'file' => $settings,
),
),
);
}
/**
* Get course handlers.
*
* @param string $type
* (optional) The course handler type to return.
* If no type is specified, all types are returned.
*
* @return array
* A merged, structured array of course handlers, optionally limited by type.
*
* @return array
* An array of hook implementations keyed by module name, containing:
* - A single handler type definition, if the $type parameter is passed.
* - Or an associative array of all course handler definitions keyed by type.
*/
function course_get_handlers($type = NULL, $flush = FALSE) {
$all = &drupal_static(__FUNCTION__, array());
if (!$all || $flush) {
// Allow modules to define handlers that extend Course functionality.
// Do not use module_invoke_all() here because we need to know which module
// is providing the 'object' handler type. This is to avoid namespace
// conflicts between multiple modules providing a 'quiz' object for example.
$hook = 'course_handlers';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$handlers = $function();
// Allow modules to alter each other's list of handlers.
drupal_alter($hook, $handlers, $module);
if (isset($handlers) && is_array($handlers)) {
$all[$module] = $handlers;
}
}
}
if (isset($type)) {
// Loop through each module's result again, and rebuild the array including
// only the specified handler type. We do this again so we can static cache
// the hook invocation and function calls above.
$filtered = array();
foreach ($all as $module => $handlers) {
if (isset($handlers[$type])) {
$filtered[$module] = $handlers[$type];
}
}
// Return the keyed array of implementations, each filtered to include only
// the specified handler type definition.
return $filtered;
}
else {
// Return the keyed array of all implementations.
return $all;
}
}
/**
* Menu access for course object router.
*/
function course_access_object($node, $courseObject) {
global $user;
$course = course_get_course($node, $user);
$courseObject->setCourse($course);
$course->setActive($courseObject->getId());
return $courseObject->access('take');
}
/**
* Fulfillment check callback.
*
* This function is polled from nav.js to check remote fulfillments for external
* learning objects.
*/
function course_ajaj_fulfullment_check($node, $courseObject, $js = FALSE) {
$courseObject->poll();
if (course_node_is_course($node)) {
course_set_context($node);
}
module_load_include('inc', 'course', 'includes/course.block');
// Bust cache.
/** @deprecated do not use getUser() */
course_get_course($node, $courseObject->getCourse()->getUser());
$block = block_load('course', 'navigation');
$block_rend = _block_render_blocks(array($block));
drupal_json_output(array(
'content' => $block_rend['course_navigation']->content['#markup'],
'complete' => $courseObject->getFulfillment()->isComplete(),
));
}
/**
* Start an editing session for this course. Populate the session from
* persistent storage.
*
* @param Course $course
* A Course.
*/
function course_editing_start($course) {
if (empty($_SESSION['course'][$course->getNode()->nid]['editing'])) {
// Start editing cache from what we have in DB.
foreach ($course->getObjects() as $courseObject) {
$_SESSION['course'][$course->getNode()->nid]['editing'][$courseObject->getId()] = $courseObject->getOptions();
}
}
}
/**
* Callback to restore a course object temporarily removed from outline overview
* form.
*/
function course_object_restore($node, $js, CourseObject $courseObject) {
$course = course_get_course($node);
$courseObject->setCourse($course);
$uniqid = $courseObject->getId();
$nid = $node->nid;
// Set the session value.
$_SESSION['course'][$nid]['editing'][$uniqid]['delete'] = 0;
$_SESSION['course'][$nid]['editing'][$uniqid]['delete_instance'] = 0;
if ($js) {
ctools_include('ajax');
// Perform ajax operations on the overview form, after restore.
$commands = array();
// Reset summary.
// @todo reload just this row. How?
//$commands[] = ajax_command_replace("#row-{$uniqid}", $html);
$commands[] = ctools_ajax_command_reload();
print ajax_render($commands);
exit;
}
else {
drupal_goto("node/$nid/course-outline");
}
}
/**
* Page callback: Handles object options form for both ctools modal and nojs.
*
* @param stdClass $node
* A course node object loaded from course_load().
* @param boolean $js
* Detects if ajax is enabled, loaded from ctools_js_load().
* @param courseObject $courseObject
* A courseObject object, loaded from course_object_load().
*
*/
function course_object_options($node, $js, $courseObject) {
$course = course_get_course($node);
$courseObject->setCourse($course);
if ($js) {
ctools_include('ajax');
ctools_include('modal');
$form_state = array(
'ajax' => TRUE,
'title' => t("Settings for %t", array('%t' => $courseObject->getTitle())),
);
$form_state['build_info']['args'][] = $courseObject;
$output = ctools_modal_form_wrapper('course_object_options_form', $form_state);
if (empty($output)) {
$output[] = ctools_modal_command_loading();
$output[] = ctools_modal_command_dismiss();
}
print ajax_render($output);
exit;
}
else {
return drupal_get_form('course_object_options_form', $courseObject);
}
}
/**
* Form API builder for course object options.
*
* @param array $form_state
* Form state.
* @param courseObject $courseObject
* An initialized courseObject object.
*
* @see course_object_options_form_validate()
* @see course_object_options_form_submit()
* @see course_object_options()
* @ingroup forms
*
* @return array
* The FAPI array.
*/
function course_object_options_form($form, &$form_state, $courseObject) {
$form = array();
$form[$courseObject->getComponent()] = array(
'#title' => $courseObject->getComponentName(),
'#type' => 'fieldset',
'#group' => 'course_tabs',
'#description' => t('Configuration for !name course objects.', array('!name' => $courseObject->getComponentName())),
'#weight' => 2,
);
$courseObject->optionsForm($form, $form_state);
field_attach_form('course_object', (object) $courseObject->getOptions(), $form, $form_state);
foreach (element_children($form) as $key) {
$element = $form[$key];
if (!empty($element['#type']) && $element['#type'] == 'container') {
$form['title'][$key] = $element;
unset($form[$key]);
}
}
$fieldset_key = $courseObject->getComponent();
foreach (element_children($form) as $key) {
$element = $form[$key];
// @todo I want to catch all object-provided fields and group them into a
// fieldset. I should probably do this with an OO design change so that we
// know where the fields are coming from. Consider adding a
// CourseObject::objectOptionsForm which will separate object-specific
// behavior from Course-specific behavior.
if (!empty($element['#type']) && !in_array($element['#type'], array('', 'hidden', 'submit', 'button', 'fieldset', 'vertical_tabs', 'value'))) {
$form[$fieldset_key][$key] = $element;
unset($form[$key]);
}
}
return $form;
}
/**
* Menu loader for course objects, in the context of a course.
*/
function course_object_load($coid) {
return course_get_course_object_by_id($coid);
}
/**
* Page handler for a course object.
*
* @return string
* Themed output.
*/
function course_object_take($courseObject) {
drupal_set_title($courseObject->getTitle());
// Preserve course tabs
$course = $courseObject->getCourse();
$item = menu_get_item($course->getUrl());
menu_set_item(NULL, $item);
if ($out = $courseObject->takeCourseObject()) {
return $out;
}
else {
drupal_access_denied();
}
}
/**
* Implements hook_block_info().
*/
function course_block_info() {
$info = array(
'outline' => array(
'info' => t('Course: Outline'),
'cache' => DRUPAL_NO_CACHE,
),
'navigation' => array(
'info' => t('Course: Navigation'),
'cache' => DRUPAL_NO_CACHE,
),
);
return $info;
}
/**
* Implements hook_block_configure().
*/
function course_block_configure($delta) {
module_load_include('inc', 'course', 'includes/course.block');
$function = "_course_block_{$delta}_configure";
if (function_exists($function)) {
return $function($delta);
}
}
/**
* Implements hook_block_view().
*/
function course_block_view($delta) {
module_load_include('inc', 'course', 'includes/course.block');
$function = "_course_block_{$delta}_view";
if (function_exists($function)) {
return $function($delta);
}
}
/**
* Implements hook_block_save().
*/
function course_block_save($delta, $edit) {
module_load_include('inc', 'course', 'includes/course.block');
$function = "_course_block_{$delta}_save";
if (function_exists($function)) {
return $function($delta);
}
}
/**
* Menu title handler for the Take course tab.
*
* @return string
* "Review course" or "Take course", depending on the current user's
* completion status.
*/
function course_takecourse_title($node) {
global $user;
$report = course_report_load($node, $user);
return ($user->uid > 1 && isset($report->complete) && $report->complete) ? t('Review course') : t('Take course');
}
/**
* Menu loader: check if node is a Course.
*/
function course_load($nid = NULL, $vid = NULL, $reset = FALSE) {
$nids = (isset($nid) ? array($nid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$node = node_load_multiple($nids, $conditions, $reset);
return $node ? (course_node_is_course(reset($node)) ? reset($node) : FALSE) : FALSE;
}
/**
* Implements hook_permission().
*
* Define permissions to take courses and edit course settings.
*/
function course_permission() {
return array(
'administer course' => array(
'title' => t('Administer global course configuration'),
'description' => t('Allows changing all course configurations.'),
'restrict access' => TRUE,
),
'access course' => array(
'title' => t('Access course'),
'description' => t('Grants access to take courses.'),
),
'access all course reports' => array(
'title' => t('Access all course reports'),
'description' => t('Grants access to all course reports across all courses.'),
),
'access course administration area' => array(
'title' => t('Access course administration area'),
'description' => t('Grants access to the course administration area, with no other permission.'),
),
'administer course enrollment types' => array(
'title' => t('Administer course enrollment types'),
'description' => t('Manage course enrollment types, fields, and display settings.'),
),
'administer course enrollments' => array(
'title' => t('Administer course enrollments'),
'description' => t('Allows editing or deleting all course enrollments.'),
),
);
}
/**
* Menu access callback to determins if the take course button should display
* on the course node.
*
* This differs from course_take_course_access() as it only returns a boolean.
*
* @param object $node
* The course node.
*
* @see course_uc_token_values()
*/
function course_take_course_menu_access($node) {
global $user;
$courses = &drupal_static(__FUNCTION__, array());
if (!isset($courses[$node->nid])) {
// Allow modules to restrict menu access to the take course tab.
$hooks = module_invoke_all('course_has_takecourse', $node, $user);
$courses[$node->nid] = !in_array(FALSE, $hooks);
}
return $courses[$node->nid];
}
/**
* @deprecated
*/
function course_take_course_access($node, $account = NULL, $flush = FALSE, $all = FALSE) {
return course_access_course('take', $node, $account, $flush, $all);
}
/**
* Callback for checking course settings permission.
*/
function course_settings_access($node) {
return node_access('update', $node);
}
/**
* Implements hook_node_view().
*/
function course_node_view($node, $view_mode, $langcode) {
if (course_node_is_course($node)) {
global $user;
$enrollment = course_enrollment_load($node->nid, $user->uid);
if ((!empty($enrollment) && $enrollment->status)) {
// User is already in course. Check take access.
$access = course_take_course_access($node, $user);
}
else {
// User not in course. Check enroll access.
$access = course_enroll_access($node, $user);
}
if (!$access['success']) {
$node->content['course_messages']['#markup'] = '<div class="course-restriction">' . "<h4>" . $access['header'] . "</h4>" . '<div class="course-restriction-message">' . $access['message'] . '</div></div>';
}
else {
// Render take course button.
$node->content['course']['#markup'] = theme('course_take_course_button', array('node' => $node));
}
}
}
/**
* Implements hook_node_insert().
*/
function course_node_insert($node) {
if (course_node_is_course($node)) {
if (!isset($node->course)) {
$node->course = array();
}
course_node_update($node);
}
}
/**
* Implements hook_node_update().
*/
function course_node_update($node) {
if (course_node_is_course($node)) {
$record = $node->course;
$record['nid'] = $node->nid;
// Add configurable dates to the node object for easy retrieval.
// Support configurable date fields.
$cck_dates = array(
'open' => 'course_start_date_' . $node->type,
'close' => 'course_expiration_date_' . $node->type,
'live_from_date' => 'course_live_from_date_' . $node->type,
'live_to_date' => 'course_live_to_date_' . $node->type,
);
// Check whether each variable is set and the field exists on the
// content type. If so, load that field's value to the course object,
// overriding the coresponding column from the database.
foreach ($cck_dates as $key => $variable) {
$settings = @unserialize(variable_get($variable, array()));
$field_exists = isset($settings['field']);
if ($field_exists) {
$items = field_get_items('node', $node, $settings['field'], $node->language);
if ($items && count($items)) {
$value = $items[0][$settings['value']];
if (!empty($value)) {
$date = new DateTime("$value UTC");
$value = $date->format('U');
}
$record[$key] = $value;
}
}
}
// 'Credits' is a numeric field so we need to ensure that it is a number.
$record['credits'] = isset($record['credits']) ? floatval($record['credits']) : 0;
// Default the enrollment type if necessary.
if (empty($record['enrollment_type'])) {
$record['enrollment_type'] = variable_get('course_default_enrollment_type');
}
$existing = db_query('SELECT 1 FROM {course_node} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField();
$update = $existing ? array('nid') : array();
drupal_write_record('course_node', $record, $update);
$node->course = $record;
// Support cloning.
course_handle_clone($node);
// Save the course objects - necessary for programmatic course creation.
if (isset($node->course['objects'])) {
$course = course_get_course($node);
course_save_objects($node->course['objects'], $course);
}
}
}
/**
* Implements hook_node_load().
*/
function course_node_load($nodes, $types) {
$result = db_query('SELECT * FROM {course_node} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$nodes[$row['nid']]->course = $row;
}
}
/**
* Implements hook_node_delete().
*/
function course_node_delete($node) {
if (course_node_is_course($node)) {
// Clean up course specific settings and enrollments when a course is
// deleted.
db_delete('course_node')
->condition('nid', $node->nid)
->execute();
db_delete('course_enrollment')
->condition('nid', $node->nid)
->execute();
db_delete('course_report')
->condition('nid', $node->nid)
->execute();
$query = db_select('course_outline', 'co');
$query->join('course_outline_fulfillment', 'cof', 'co.coid = cof.coid');
$result = $query
->fields('co')
->condition('co.nid', $node->nid)
->execute();
while ($row = $result->fetch()) {
db_delete('course_outline_fulfillment')
->condition('coid', $row->coid)
->execute();
}
db_delete('course_outline')
->condition('nid', $node->nid)
->execute();
}
}
/**
* Saves course objects.
*
* @param CourseObject[] $objects
* An array of course object definitions.
* @param Course $course
* (optional) An instantiated Course, from course_get_course().
*/
function course_save_objects(array $objects, Course $course = NULL) {
foreach ($objects as $object) {
// Check if this course object already exists in the database.
if (isset($object->coid)) {
// Check if this object does not belong to the current node.
if ($object->nid != $course->getNode()->nid) {
// We are importing or cloning. Ensure the necessary keys are empty,
// in order to prepare a new object using this object's definitions.
$unset = array('coid', 'nid', 'uuid', 'uniqid');
foreach ($unset as $key) {
if (isset($object->{$key})) {
unset($object->{$key});
}
}
// Replace the nid key, to properly associate the current course node
// with this course object.
$object->nid = $course->getNode()->nid;
}
}
$object->save();
}
}
/**
* Enrolls a user in a course.
*
* @param object $node
* By reference. The course node.
* @param object $user
* By reference. The enrolling user.
* @param string $from
* The type of enrollment, if applicable. {course_enrollment}.enrollmenttype.
* @param string $code
* The access code used to enroll. {course_enrollment}.code.
* @param integer $status
* The enrollment status. {course_enrollment}.status.
*/
function course_enroll($node, $account, $from = NULL, $code = NULL, $status = 1) {
if (!$account) {
global $user;
$account = $user;
}
if (course_node_is_course($node)) {
$enrollment = entity_create('course_enrollment', array(
'nid' => $node->nid,
'uid' => $account->uid,
'enrollmenttype' => $from,
'status' => $status,
'code' => $code,
'type' => $node->course['enrollment_type'],
));
$watchdog_variables = array(
'!uid' => $account->uid,
'!nid' => $node->nid,
);
if (!course_enrollment_check($node->nid, $account->uid)) {
// User is not enrolled yet.
watchdog('course_enroll', 'Enrolling user !uid into !nid.', $watchdog_variables);
$enrollment->save();
}
// Flush caches.
course_access_course('take', $node, $account, TRUE);
return $enrollment;
}
else {
return FALSE;
}
}
/**
* Un-enroll the user.
*
* Deletes course report entries, course enrollments, and object fulfillment
* records.
*
* @param object $node
* A course node.
* @param object $user
* A user.
* @return bool
* TRUE if user is un-enrolled, FALSE if node is not a course.
*/
function course_unenroll($node, $user) {
if ($enrollment = course_enrollment_load($node->nid, $user->uid)) {
return $enrollment->delete();
}
}
/**
* Check if the user has enrolled in a course.
*
* @param mixed $nid
* A course node ID.
* @param mixed $uid
* A user ID.
*
* @return bool
* TRUE if the user is enrolled, FALSE otherwise.
*/
function course_enrollment_check($nid, $uid) {
$checked = &drupal_static(__FUNCTION__, array());
if (!isset($checked[$nid][$uid])) {
$query = db_query("SELECT 1 FROM {course_enrollment} WHERE nid = :nid AND uid = :uid AND status = :status", array(':nid' => $nid, ':uid' => $uid, ':status' => 1));
$status = ($query->fetchField() > 0);
$checked[$nid][$uid] = $status;
}
return $checked[$nid][$uid];
}
/**
* Load an enrollment from a node ID and user ID.
*
* @param int $nid
* Enrollment ID, or node ID.
* @param int $uid
* User ID.
*
* @return CourseEnrollment
* Enrollment object or FALSE
*/
function course_enrollment_load($nid, $uid = NULL) {
if (is_object($nid)) {
$nid = $nid->nid;
}
if (is_null($uid)) {
$eid = $nid;
return entity_load_single('course_enrollment', $eid);
}
if (is_object($uid)) {
$uid = $uid->uid;
}
if ($enrollment = db_query("SELECT * FROM {course_enrollment} WHERE nid = :nid AND uid = :uid", array(':nid' => $nid, ':uid' => $uid))->fetch()) {
return entity_load_single('course_enrollment', $enrollment->eid);