cis_example_cis.module

  1. cis7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
  2. cle7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
  3. ecd7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
  4. elmsmedia7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
  5. harmony7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
  6. icor7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
  7. meedjum_blog7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
  8. mooc7 sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module

Code for the Example CIS feature.

Functions

File

sites/all/modules/_my_modules/cis_examples/cis_example_cis/cis_example_cis.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Code for the Example CIS feature.
  5. */
  6. include_once 'cis_example_cis.features.inc';
  7. /**
  8. * Implements hook_entity_presave().
  9. */
  10. function cis_example_cis_entity_presave($entity, $type) {
  11. // potentially alter the data of sections just before saving
  12. // only do this if dates are not set, this happens when built programatically
  13. if ($type == 'field_collection_item' && $entity->field_name == 'field_sections' && empty($entity->field_access_dates)) {
  14. $dates = _cis_example_utility_university_dates($entity->HostEntity()->field_semester['und'][0]['value'], $entity->HostEntity()->field_year['und'][0]['value']);
  15. // verify that we actually got valid dates
  16. if (!empty($dates)) {
  17. // value is start date
  18. $entity->field_access_dates['und'][0]['value'] = $dates['start'];
  19. // value2 is end date
  20. $entity->field_access_dates['und'][0]['value2'] = $dates['end'];
  21. // class officially starts a week into duration
  22. $entity->field_course_start['und'][0]['value'] = $dates['class_begin'];
  23. }
  24. }
  25. }
  26. /**
  27. * Implements hook_cis_quick_submit_alter().
  28. */
  29. function cis_example_cis_cis_quick_submit_alter(&$section, &$offering) {
  30. // load the start / end date based on university calender
  31. $dates = _cis_example_utility_university_dates($offering->field_semester['und'][0]['value'], $offering->field_year['und'][0]['value']);
  32. // verify that we actually got valid dates
  33. if (!empty($dates)) {
  34. // value is start date
  35. $section->field_access_dates[LANGUAGE_NONE][0]['value'] = $dates['start'];
  36. // value2 is end date
  37. $section->field_access_dates[LANGUAGE_NONE][0]['value2'] = $dates['end'];
  38. // class officially starts a week into duration
  39. $section->field_course_start[LANGUAGE_NONE][0]['value'] = $dates['class_begin'];
  40. }
  41. }
  42. /**
  43. * Implements hook_form_alter().
  44. */
  45. function cis_example_cis_form_alter(&$form, &$form_state, $form_id) {
  46. // alter settings specific to CIS on quick launch page
  47. if ($form_id == 'cis_helper_quick_setup_page') {
  48. // convert campus to select field with options
  49. $form['details']['section']['campus']['#type'] = 'select';
  50. $form['details']['section']['campus']['#options'] = _cis_example_utility_campus_list();
  51. // handle LTI request if one is detected
  52. if (isset($_GET['lti_launch'])) {
  53. $form['course']['#visible'] = FALSE;
  54. $lti_launch = drupal_json_decode(str_replace('&quot;', '"', $_GET['lti_launch']));
  55. // convert to only the values we care about for Example
  56. $lti = _cis_example_utility_lti_translation($lti_launch);
  57. // account for section string set but not course
  58. if (!empty($lti['section']['string']) && empty($lti['course']['machine_name'])) {
  59. $exp = explode('___', $lti['section']['string']);
  60. $exp = explode('__', $exp[1]);
  61. $lti['course']['name'] = $exp[0];
  62. $exp[1] = str_replace('_', '', substr($exp[1], 0, count($exp[1]) - 4));
  63. $lti['course']['name'] = $exp[0] . ' ' . $exp[1];
  64. $lti['course']['machine_name'] = strtolower($exp[0] . $exp[1]);
  65. }
  66. $populated = array();
  67. // convert to hidden element since this was just populated from url
  68. $form['course']['#type'] = 'hidden';
  69. // prepopulate course to create based on LTI request
  70. if (array_key_exists($lti['course']['machine_name'], $form['course']['#options'])) {
  71. $form['course']['#default_value'] = $lti['course']['machine_name'];
  72. $populated[] = array(t('Course'), $lti['course']['name'] . ' : ' . $lti['course']['title']);
  73. }
  74. else {
  75. // this is a new course so populate form
  76. $form['new_course']['new_name']['#default_value'] = $lti['course']['name'];
  77. // set course title
  78. $form['new_course']['new_title']['#default_value'] = $lti['course']['title'];
  79. $populated[] = array(t('Course'), $lti['course']['name'] . ' : ' . $lti['course']['title']);
  80. // set access to lms if this came from lms
  81. if ($lti['source']['name'] == 'lms') {
  82. $form['new_course']['access']['#default_value'] = 'lms';
  83. $populated[] = array(t('Access'), t('Students in my lms section'));
  84. }
  85. // collapse new course form
  86. $form['new_course']['#collapsed'] = TRUE;
  87. }
  88. // collapse details area by default to clean up form
  89. $form['details']['#collapsed'] = TRUE;
  90. // set default service to course flow
  91. $services = _cis_helper_assemble_entity_list('node', 'service', 'field_machine_name', 'nid');
  92. // currently, course is the only default
  93. // TODO: make a UI as to when to use this
  94. $form['details']['services']['#default_value'] = array($services['courses']);
  95. // populate the offering semester
  96. $form['details']['offering']['semester']['#default_value'] = $lti['section']['semester'];
  97. // collapse offering
  98. $form['details']['offering']['#collapsed'] = TRUE;
  99. $populated[] = array(t('Semester'), $lti['section']['semester']);
  100. // populate the offering year
  101. $form['details']['offering']['year']['#default_value'] = $lti['section']['year'];
  102. $populated[] = array(t('Year'), $lti['section']['year']);
  103. // collapse section
  104. $form['details']['section']['#collapsed'] = TRUE;
  105. // populate section information
  106. $form['details']['section']['access_string']['#default_value'] = $lti['section']['string'];
  107. $populated[] = array(t('lms section'), $lti['section']['string']);
  108. // populate instructor from LTI launch user
  109. $form['details']['section']['instructor']['#default_value'] = $lti['user']['name'];
  110. $populated[] = array(t('Instructor'), $lti['user']['name']);
  111. // set section number
  112. $form['details']['section']['number']['#default_value'] = $lti['section']['number'];
  113. $populated[] = array(t('Section number'), $lti['section']['number']);
  114. // set campus code
  115. $form['details']['section']['campus']['#default_value'] = $lti['section']['campus'];
  116. $populated[] = array(t('Campus code'), $lti['section']['campus']);
  117. // add additional js to hide these elements from view
  118. //drupal_add_js(drupal_get_path('module', 'cis_example_cis') .'/js/cis_example_cis.js');
  119. // add section to top of form indicating what was prepopulated
  120. $output = '';
  121. foreach($populated as $values) {
  122. $output .= '<strong>' . $values[0] . '</strong> - ' . $values[1] . '<br />';
  123. }
  124. $form['launch_details'] = array(
  125. '#markup' => $output,
  126. '#weight' => -10,
  127. );
  128. // create an advanced container as lti calls are all advanced
  129. // if changed from the default state
  130. $form['_cis_advanced'] = array(
  131. '#description' => t('Only modify these if you have reason to do so.'),
  132. '#collapsed' => '1',
  133. '#type' => 'fieldset',
  134. '#collapsible' => '1',
  135. '#title' => t('Advanced'),
  136. );
  137. $form['_cis_advanced']['new_course'] = $form['new_course'];
  138. unset($form['new_course']);
  139. $form['_cis_advanced']['details'] = $form['details'];
  140. unset($form['details']);
  141. }
  142. }
  143. // get official Examples dates
  144. if ($form_id == 'field_collection_item_form') {
  145. // only load if this is a new section
  146. if (is_null($form_state['build_info']['args'][0]->item_id)) {
  147. // load offering from the address
  148. $offerings = entity_load('field_collection_item', array(arg(4)));
  149. // only 1 item so pop it off for access
  150. $offering = array_pop($offerings);
  151. // load the start / end date based on university calender
  152. if (isset($offering)) {
  153. $dates = _cis_example_utility_university_dates($offering->field_semester['und'][0]['value'], $offering->field_year['und'][0]['value']);
  154. // verify that we actually got valid dates
  155. if (!empty($dates)) {
  156. // value is start date
  157. $form['field_access_dates']['und'][0]['#default_value']['value'] = $dates['start'];
  158. // value2 is end date
  159. $form['field_access_dates']['und'][0]['#default_value']['value2'] = $dates['end'];
  160. // class officially starts a week into duration
  161. $form['field_course_start']['und'][0]['#default_value']['value'] = $dates['class_begin'];
  162. }
  163. }
  164. }
  165. }
  166. // set campus to example specific ones on field form
  167. if ($form_id == 'field_ui_field_edit_form' && isset($form_state['field']['field_campus'])) {
  168. $campuses = _cis_example_utility_campus_list();
  169. $values = array();
  170. foreach ($campuses as $code => $name) {
  171. $values[] = $code . '|' . $name;
  172. }
  173. $form['field']['settings']['allowed_values']['#default_value'] = implode("\n", $values);
  174. }
  175. }
  176. /**
  177. * Implements hook_cis_service_instance_options_alter().
  178. */
  179. function cis_example_cis_cis_service_instance_options_alter(&$options, $course, $service) {
  180. // disable pathauto
  181. $options['dis'][] = 'pathauto';
  182. $options['dis'][] = 'path';
  183. // uninstall these items
  184. $options['pm-uninstall'][] = 'pathauto';
  185. $options['pm-uninstall'][] = 'path';
  186. // enable example authentication / connection mechanisms
  187. $options['en'][] = 'cis_example_utility';
  188. $options['en'][] = 'cis_example_cis_service';
  189. // check on method of access for potential features to revert
  190. if (in_array($course->field_method_of_access['und'][0]['value'], array('authenticated', 'code', 'lms'))) {
  191. $options['fr'][] = 'cis_account_required';
  192. if ($course->field_method_of_access['und'][0]['value'] != 'authenticated') {
  193. $options['fr'][] = 'cis_lms_required';
  194. }
  195. }
  196. // allow users to register without approval
  197. $options['vset'][] = 'user_register 1';
  198. // don't require email verification
  199. $options['vset'][] = 'user_email_verification 0';
  200. // preprocess defaults
  201. $options['vset'][] = 'preprocess_css 1';
  202. $options['vset'][] = 'preprocess_js 1';
  203. // set the private files directory automatically, specific to course name
  204. $options['vset'][] = 'file_private_path /var/www/elmsln/config/private_files/' . $service->field_machine_name[LANGUAGE_NONE][0]['value'] . '/' . $course->field_machine_name[LANGUAGE_NONE][0]['value'];
  205. // for mooc enable these specific modifications to the university
  206. if ($service->field_distribution['und'][0]['value'] == 'mooc') {
  207. $options['en'][] = 'cis_example_mooc';
  208. // require WCAG 2.0
  209. $options['en'][] = 'textbook_wcag20aa';
  210. }
  211. }
  212. /**
  213. * Implements hook_profiler_builder_drush_modules_list_alter().
  214. */
  215. function cis_example_cis_profiler_builder_drush_modules_list_alter(&$project_data, $machine_name) {
  216. // remove all modules listed as part of Examples package
  217. unset($project_data['Examples']);
  218. }
  219. /**
  220. * Implements hook_profiler_builder_modules_list_alter
  221. */
  222. function cis_example_cis_profiler_builder_modules_list_alter(&$modules) {
  223. // remove this module
  224. unset($modules['cis_example_cis']);
  225. }
  226. /**
  227. * Connect to LMS data source
  228. */
  229. function _cis_example_cis_connect($data, $bucket = 'lms', $location = 'address', $api = 'exampleapi') {
  230. // make sure we passed it a valid API
  231. $settings = _cis_connector_build_registry($bucket);
  232. if (in_array($api, $settings['apis'])) {
  233. $args = array(
  234. 'exampleuser' => $settings['user'],
  235. 'examplepassword' =>$settings['pass'],
  236. 'exampleaction' => $api,
  237. ) + $data;
  238. $address = $settings[$location] . '?' . http_build_query($args);
  239. httprl_request($address);
  240. // send the request off
  241. $request = httprl_send_request();
  242. $request = array_pop($request);
  243. return $request->data;
  244. }
  245. return FALSE;
  246. }
  247. /**
  248. * Implements hook_menu().
  249. */
  250. function cis_example_cis_menu() {
  251. $items['cis_example_get_course_list'] = array(
  252. 'title' => '!inst lms sections for !sem',
  253. 'title arguments' => array('!inst' => 1, '!sem' => 2),
  254. 'page callback' => 'drupal_get_form',
  255. 'page arguments' => array('_cis_example_cis_get_course_list'),
  256. 'access arguments' => array('access cis get course list'),
  257. 'type' => MENU_NORMAL_ITEM,
  258. );
  259. return $items;
  260. }
  261. /**
  262. * Implements hook_permission().
  263. */
  264. function cis_example_cis_permission() {
  265. return array(
  266. 'access cis get course list' => array(
  267. 'title' => t('Access CIS Get Course List'),
  268. ),
  269. );
  270. }
  271. /**
  272. * Style results from the instructor query
  273. */
  274. function _cis_example_cis_get_course_list($form, $form_state) {
  275. $username = arg(1);
  276. $semester = arg(2);
  277. if (!empty($username)) {
  278. module_load_include('inc', 'cis_example_cis', 'DOMToArrayDocument');
  279. $data = array(
  280. 'USER_ID' => check_plain(drupal_strtoupper($username)),
  281. 'SEMESTER' => check_plain($semester),
  282. );
  283. // return xml list of course data
  284. $results = _cis_example_cis_connect($data, 'lms', 'address', 'exampleapi');
  285. $dom = new DOMToArrayDocument();
  286. $dom->loadXml($results);
  287. $section_result = $dom->toArray();
  288. // make sure the request was valid
  289. if ($section_result['Result']['success'] != 'False') {
  290. $enrollments = $section_result['Result']['enrollment'];
  291. $header = array('Title', 'Course ID', 'Semester', 'Status', 'Source', 'Setup');
  292. // check for 1 result as no need to deep loop
  293. if (isset($enrollments['title'])) {
  294. $title = $enrollments['title'];
  295. $course_id = $enrollments['course_id'];
  296. $semester = $enrollments['semester'];
  297. // convert t/f to status statement
  298. if ($enrollments['active'] == 'True') {
  299. $status = 'Active';
  300. }
  301. else {
  302. $status = 'Inactive';
  303. }
  304. $source_courses = '';
  305. if (isset($enrollments['source_courses']['source_id'])) {
  306. foreach ($enrollments['source_courses']['source_id'] as $sc) {
  307. $source_courses .= $sc .' ';
  308. }
  309. }
  310. $build_link = l(t('Setup section'), 'cis-quick-setup-access-id/' . $course_id);
  311. $rows[] = array($title, $course_id, $semester, $status, $source_courses, $build_link);
  312. }
  313. else {
  314. // loop through each record and convert to table format
  315. foreach ($enrollments as $section) {
  316. $title = $section['title'];
  317. $course_id = $section['course_id'];
  318. $semester = $section['semester'];
  319. // convert t/f to status statement
  320. if ($section['active'] == 'True') {
  321. $status = 'Active';
  322. }
  323. else {
  324. $status = 'Inactive';
  325. }
  326. $source_courses = '';
  327. if (isset($section['source_courses']['source_id'])) {
  328. foreach ($section['source_courses']['source_id'] as $sc) {
  329. $source_courses .= $sc .' ';
  330. }
  331. }
  332. $build_link = l(t('Setup section'), 'cis-quick-setup-access-id/' . $course_id);
  333. $rows[] = array($title, $course_id, $semester, $status, $source_courses, $build_link);
  334. }
  335. }
  336. $form['markup'] = array(
  337. '#markup' => theme('table', array('header' => $header, 'rows' => $rows)),
  338. );
  339. }
  340. else {
  341. $form['markup'] = array(
  342. '#markup' => t('No results found for that semester / instructor'),
  343. );
  344. }
  345. }
  346. else {
  347. $form['markup'] = array(
  348. '#markup' => t('Instructor is required, semester is preferred'),
  349. );
  350. }
  351. // prepare an inline form since no values were passed as arguments
  352. $form['username'] = array(
  353. '#title' => t('Instructor'),
  354. '#description' => t('Instructor to look up courses for'),
  355. '#type' => 'textfield',
  356. '#required' => TRUE,
  357. '#default_value' => $username,
  358. );
  359. $semesters = array(
  360. '' => 'Select a semester',
  361. '201314FA' => 'Fall 13-14',
  362. '201314SP' => 'Spring 13-14',
  363. '201314S1' => 'Summer 13-14',
  364. '201415FA' => 'Fall 14-15',
  365. '201415SP' => 'Spring 14-15',
  366. '201415S1' => 'Summer 14-15',
  367. '201516FA' => 'Fall 15-16',
  368. '201516SP' => 'Spring 15-16',
  369. '201516S1' => 'Summer 15-16',
  370. '201617FA' => 'Fall 16-17',
  371. '201617SP' => 'Spring 16-17',
  372. '201617S1' => 'Summer 16-17',
  373. '201718FA' => 'Fall 17-18',
  374. '201718SP' => 'Spring 17-18',
  375. '201718S1' => 'Summer 17-18',
  376. );
  377. $form['semester'] = array(
  378. '#type' => 'select',
  379. '#options' => $semesters,
  380. '#title' => t('Semester'),
  381. '#default_value' => $semester,
  382. '#description' => t('Semester to look up'),
  383. );
  384. $form['actions']['submit'] = array(
  385. '#type' => 'submit',
  386. '#value' => t('Lookup sections'),
  387. );
  388. return $form;
  389. }
  390. /**
  391. * Submit handler for _cis_example_cis_get_course_list.
  392. */
  393. function _cis_example_cis_get_course_list_submit($form, $form_state) {
  394. $link = $form_state['values']['username'];
  395. if (!empty($form_state['values']['semester'])) {
  396. $link .= '/' . $form_state['values']['semester'];
  397. }
  398. // redirect to part of path that provides the filtering
  399. drupal_goto("cis_example_get_course_list/$link");
  400. }
  401. /**
  402. * Implements hook_admin_paths().
  403. */
  404. function cis_example_cis_admin_paths() {
  405. $paths = array(
  406. 'cis_example_get_course_list/*' => TRUE,
  407. );
  408. return $paths;
  409. }