function get_accessible_surveys() { // Get names of all the survey responses (from other users) that are accessible to our user... global $USER; $results = get_records_sql_array("SELECT\ta.title\n\t\tFROM {artefact} a\n\t\tLEFT JOIN {artefact_access_usr} aau ON (aau.artefact = a.id)\n\t\tWHERE aau.usr = ?", array($USER->get('id'))); // Prepare our accessible surveys array so we can use it as options in drop-down select box... $survey_types = array('empty' => get_string('selectsurvey', 'artefact.survey')); if ($results) { // There can be (and often will be) duplicates of survey names: // two/more users can complete the same survey, but the responses will appear as different artefacts. // // We use/simulate mathematical concept of Set, which can contain only one instance of the same thing (survey in our case). // // We achieve that by checking if the current survey name is already contained in our accessible surveys array. // If not, we add the name of taht survey to our accessible surveys array $accessible_surveys = array(); foreach ($results as $result) { if (!in_array($result->title, $accessible_surveys)) { $accessible_surveys[] = $result->title; } } foreach ($accessible_surveys as $filename) { $LANGUAGE = ArtefactTypeSurvey::get_default_lang($filename); $xmlDoc = new DOMDocument('1.0', 'UTF-8'); $ch = curl_init(get_config('wwwroot') . 'artefact/survey/surveys/' . $filename); # Return http response in string curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $loaded = $xmlDoc->loadXML(curl_exec($ch)); if ($loaded) { $surveyname = $xmlDoc->getElementsByTagName('survey')->item(0)->getAttribute('name'); if (isset($surveyname) && $surveyname == substr($filename, 0, -4)) { $title = $xmlDoc->getElementsByTagName('title')->item(0)->getAttribute($LANGUAGE); $survey_types = array_merge($survey_types, array($filename => $title)); } else { $message = get_string('surveynameerror', 'artefact.survey', $filename); $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message); } } else { $message = get_string('surveyerror', 'artefact.survey', $filename); $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message); } } } return $survey_types; }
function getoptions_available_surveys() { // Get names of all the survey responses (from other users) that are accessible to our user... global $USER; $results = get_records_sql_array("SELECT\ta.title\n\t\tFROM {artefact} a\n\t\tWHERE a.artefacttype = 'survey' AND a.owner = ?", array($USER->get('id'))); if (!$results) { return false; } $available_surveys = array(); foreach ($results as $result) { if (!in_array($result->title, $available_surveys)) { $available_surveys[] = $result->title; } } // Prepare our available surveys array so we can use it as options in drop-down select box... $survey_types = array(); foreach ($available_surveys as $filename) { $LANGUAGE = ArtefactTypeSurvey::get_default_lang($filename); $xmlDoc = new DOMDocument('1.0', 'UTF-8'); $ch = curl_init(get_config('wwwroot') . 'artefact/survey/surveys/' . $filename); # Return http response in string curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $loaded = $xmlDoc->loadXML(curl_exec($ch)); if ($loaded) { $surveyname = $xmlDoc->getElementsByTagName('survey')->item(0)->getAttribute('name'); if (isset($surveyname) && $surveyname == substr($filename, 0, -4)) { $charttype = $xmlDoc->getElementsByTagName('chart')->item(0); if (!empty($charttype)) { $title = $xmlDoc->getElementsByTagName('title')->item(0)->getAttribute($LANGUAGE); $survey_types = array_merge($survey_types, array($filename => $title)); } } else { $message = get_string('surveynameerror', 'artefact.survey', $filename); $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message); } } else { $message = get_string('surveyerror', 'artefact.survey', $filename); $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message); } } return $survey_types; }
function getoptions_surveys() { $survey_types = array(); // Read all the filenames of xml files in surveys sub-folder // and also read survey title from each of xml files if ($handle = opendir(get_config('docroot') . 'artefact/survey/surveys')) { // This is the correct way to loop over the directory. while (false !== ($filename = readdir($handle))) { // Only XML files... if (substr($filename, -3) == 'xml') { //$LANGUAGE = get_config('lang'); $LANGUAGE = ArtefactTypeSurvey::get_default_lang($filename); $xmlDoc = new DOMDocument('1.0', 'UTF-8'); $ch = curl_init(get_config('wwwroot') . 'artefact/survey/surveys/' . $filename); # Return http response in string curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $loaded = $xmlDoc->loadXML(curl_exec($ch)); if ($loaded) { $surveyname = $xmlDoc->getElementsByTagName('survey')->item(0)->getAttribute('name'); if (isset($surveyname) && $surveyname == substr($filename, 0, -4)) { $title = $xmlDoc->getElementsByTagName('title')->item(0)->getAttribute($LANGUAGE); // If the survey title doesn't exist in selected language, than set the survey title in English /* if (empty($title)) { $title = $xmlDoc->getElementsByTagName('title')->item(0)->getAttribute(self::get_default_lang($filename)); } */ $survey_types = array_merge($survey_types, array($filename => $title)); } else { $message = get_string('surveynameerror', 'artefact.survey', $filename); $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message); } } else { $message = get_string('surveyerror', 'artefact.survey', $filename); $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message); } } } closedir($handle); } return $survey_types; }