<?php

/**
 * 
 * This PHP script is invoked by Moodle every time it enters at Admin section (./admin/search.php).
 * It provides a link to the NASA-TLX/LInE general configurations area ('pluginname').
 * 
 * @package   mod_nasatlx
 * @author Leônidas O. Brandão
 * @version v 0.1 2019/03/04
 * @category  
 * @copyright 2014 LInE - http://line.ime.usp.br
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

// Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
defined('MOODLE_INTERNAL') || die();

global $OUTPUT, $CFG, $DB;
require_once($CFG->dirroot . '/mod/nasatlx/lib.php');
require_once($CFG->dirroot . '/mod/nasatlx/locallib.php');

$action = optional_param('action', 'view', PARAM_TEXT);
$ilm_id = optional_param('ilm_id', 0, PARAM_INT);
$ilm_param_id = optional_param('ilm_param_id', 0, PARAM_INT);
$ilm_id_parent = optional_param('ilm_id_parent', 0, PARAM_INT);
$status = optional_param('status', 0, PARAM_INT);

if (!$action && !$ilm_id) {
  return; // nothing to be done (it is the Admin entering at the initial administrative section)
  }

//D echo "/mod/nasatlx/settings.php: action=$action<br/>";
/*
if ($action == 'visible') {
  ilm_settings::visible_ilm($ilm_id, $status);
  $action = 'config';
  $ilm_id = $ilm_id_parent;
  }
else
if ($action == 'visible_param') {
  ilm_settings::visible_param($ilm_param_id, $status);
  $action = 'config';
  }
else
if ($action == 'export') {
  ilm_settings::export_ilm($ilm_id);
  $action = 'config';
  }
*/
$str = '';
//D if (!file_exists($CFG->dirroot . '/blocks/nasatlx_block/version.php')) {
//D   $str .= $OUTPUT->box_start();
//D   $str .= '<center>' . $OUTPUT->error_text(get_string('error_check_nasatlx_block', 'nasatlx')) . '</center>';
//D   $str .= $OUTPUT->box_end();
//D   }

if ($action == 'view') {
  //  echo "settings.php: action==view<br/>"; exit;
  $url_export_all = new moodle_url('/mod/nasatlx/settings_nasatlx_line.php', array('action' => 'export'));
  $action_add = new popup_action('click', $url_export_all, 'popup', array('width' => 900, 'height' => 650));
  $link_export_all = $OUTPUT->action_link($url_export_all, get_string('export_all_nasatlx', 'nasatlx'), $action_add) . $OUTPUT->help_icon('export_all_nasatlx', 'nasatlx');

  //TODO Implement export all questionnaires NASA-TLX/LInE and their associated data (all of them)
  //DDD $url_import = new moodle_url('/mod/nasatlx/settings_nasatlx_line.php', array('action' => 'import'));
  //DDD $action_import = new popup_action('click', $url_import, 'popup', array('width' => 900, 'height' => 650));
  //DDD $link_import = $OUTPUT->action_link($url_import, get_string('export_nasatlx', 'nasatlx'), $action_import) . $OUTPUT->help_icon('export_nasatlx', 'nasatlx');

  // First list all iLM from type HTML
  // Table '*_nasatlx'      : id course name intro introformat timecreated timemodified 
  // Table '*_nasatlx_resps': id scale1 scale2 scale3 scale4 scale5 calc1 calc2 calc3 calc4 calc5 peso1 peso2 peso3 peso4 peso5 cmid course user created

  $nasatlx_list = $DB->get_records_sql("SELECT * FROM {nasatlx} WHERE 1 = 1");
  //D foreach ($nasatlx_list as $nasa_item) echo " * " . $nasa_item->id . ", " . $nasa_item->course . ", " . $nasa_item->name . ", " . $nasa_item->intro . "<br/>";

  $str .= '<table id="outlinetable" class="generaltable boxaligncenter" cellpadding="5" width="100%">' . chr(13);
  $str .= '<tr><td colspan=2 align=left>' . $link_export_all . '</td>'; // must be implemented in 'settings_nasatlx_line.php'
  // $str .= '<td colspan=2 align=right>' . $link_import . '</td></tr>';

  if ($nasatlx_list) {

    foreach ($nasatlx_list as $item_quest) {
      $url_view_quest = new moodle_url('/admin/settings.php', array('section' => 'modsettingnasatlx', 'action' => 'config', 'ilm_id' => $item_quest->id));
      //DDD $link_config = $OUTPUT->action_link($url_view_quest, nasatlx_icons::insert('config_ilm'));

      $str .= '  <tr>' . chr(13);
      $str .= '    <td class="header c1" width=75% title="fields: name, description"><strong>' . $item_quest->name . ' (' . $item_quest->id . ')</strong></td>' . chr(13);

      $str .= '<td class="header c1" width=10% ><strong title="course id">' . $item_quest->course . '</strong></td>' . chr(13);
      //TODO implement 'search_total_of_answers($id)': return the total of answers in this questionnaire
      // $str .= '<td class="header c1" width=10% ><strong title="number of answers">' . search_total_of_answers($item_quest->id) . ':</strong></td>' . chr(13);
      $str .= '</tr>';
      } // foreach ($nasatlx_list as $item_quest)

    } // if ($nasatlx_list)
  $str .= '</table>';

  $settings->add(new admin_setting_heading('nasatlx', get_string('view_nasatlx', 'nasatlx') . $OUTPUT->help_icon('modulename', 'nasatlx'), $str));

  } // if ($action == 'view')
else if ($action == 'confirm_upgrade') {
  //D print "settings.php: 1 action==confirm_upgrade - to be implemented<br/>";
  if (is_debugging()) print "nasatlx: settings.php: action==confirm_upgrade - to be implemented<br/>";
  //  debugging("settings.php: action==confirm_upgrade - to be implemented<br/>", DEBUG_DEVELOPER);
  } // else if ($action == 'confirm_upgrade')

else if ($action == 'config') { // Administration > plugins > NASA-TLX/LInE : after select the iLM reaches this point
  //D print "settings.php: action==config - to be implemented<br/>";
  //debugging("settings.php: action==config - to be implemented<br/>", DEBUG_DEVELOPER);
  //D if (is_debugging()) // declared in 'lib.php' // NOT WORKING!!!!
  //D  print "<br/>&bnsp;<br/>&bnsp;<br/>&bnsp;<br/>&bnsp;<br/>&bnsp;<br/>&bnsp;<br/>&bnsp;nasatlx: settings.php: action==config - to be implemented<br/>";
  }