/**
  * Will save the given data, that should be from calling the get_data function. Data will be all of the assessments in the course
  *
  * Also handles removing elements that should be deleted from the form.
  *
  * @param object $data value from calling get_data on this form
  *
  */
 function save_assessment_list($data)
 {
     global $DB;
     $changed = array('assessmentname', 'type', 'assessmentprof', 'assessmentexamtype', 'assessmentduedate', 'description', 'gdescription', 'assessmentweight', 'was_deleted');
     $learningObjectiveTypes = get_learning_objective_types();
     foreach ($learningObjectiveTypes as $learningObjectiveType) {
         $changed[] = 'learning_objective_' . $learningObjectiveType;
     }
     $assessment_types = get_assessment_types();
     $exam_types = get_exam_types();
     $convertedAttributes = array('type' => function ($value) use($assessment_types) {
         return $assessment_types[$value];
     }, 'assessmentexamtype' => function ($value) use($exam_types) {
         return $exam_types[$value];
     });
     $assessment_parser = new recurring_element_parser('courseassessment', 'assessment_list', $changed, $convertedAttributes);
     $tuples = $assessment_parser->getTuplesFromData($data);
     foreach ($tuples as $tupleKey => $tuple) {
         // TODO: Should also delete all the relations for each assessment
         // If the tuple has been deleted, then remove it from the database
         if ($tuple['was_deleted'] == true) {
             $assessment_parser->deleteTupleFromDB($tuple);
             // Finally, remove it from the tuples that will be saved, because otherwise will just be resaved anyway
             unset($tuples[$tupleKey]);
         }
     }
     $assessment_parser->saveTuplesToDB($tuples);
     foreach ($tuples as $tuplekey => $tuple) {
         $learningObjectiveTypes = get_learning_objective_types();
         foreach ($learningObjectiveTypes as $learningObjectiveType) {
             $key = 'learning_objective_' . $learningObjectiveType;
             if (array_key_exists($key, $tuple) and is_array($tuple[$key])) {
                 foreach ($tuple[$key] as $objectiveId) {
                     $newLink = new stdClass();
                     $newLink->assessmentid = $tuple['id'];
                     $newLink->objectiveid = $objectiveId;
                     print_object($newLink);
                     $DB->insert_record('assessmentobjectives', $newLink, false);
                 }
             }
         }
     }
 }
 /**
  * Will save the given data, that should be from calling the get_data function. Data will be all of the sessions in the course
  *
  * Also handles removing elements that should be deleted from the form.
  *
  * @param object $data value from calling get_data on this form
  *
  */
 public function save_data($data)
 {
     global $DB;
     // Set up the recurring element parser
     $allChangedAttributes = array('sessiontitle', 'sessionteachingstrategy', 'sessionguestteacher', 'sessiontype', 'sessionlength', 'sessiondate', 'assessments', 'was_deleted', 'all_topics_text_array');
     $learningObjectiveTypes = get_learning_objective_types();
     foreach ($learningObjectiveTypes as $learningObjectiveType) {
         $allChangedAttributes[] = 'learning_objective_' . $learningObjectiveType;
     }
     $types = get_session_types();
     $lengths = get_session_lengths();
     $strategies = get_teaching_strategies();
     $convertedAttributes = array('sessiontype' => function ($value) use($types) {
         return $types[$value];
     }, 'sessionlength' => function ($value) use($lengths) {
         return $lengths[$value];
     });
     $session_recurring_parser = new recurring_element_parser('coursesession', 'sessions_list', $allChangedAttributes, $convertedAttributes);
     // Get the tuples (one for each session) from the parser
     $tuples = $session_recurring_parser->getTuplesFromData($data);
     // Handles deleting a session
     foreach ($tuples as $tupleKey => $tuple) {
         // Clean out the sessionobjectives and session_related_assessment for this session
         $this->delete_all_relations_to_session($tuple['id']);
         // If the tuple has been deleted, then remove it from the database
         if ($tuple['was_deleted'] == true) {
             $session_recurring_parser->deleteTupleFromDB($tuple);
             // Finally, remove it from the tuples that will be saved, because otherwise will just be resaved anyway
             unset($tuples[$tupleKey]);
         }
     }
     // Save the remaining data for the sessions/tuples
     // Will also update the id for elements that are new
     $session_recurring_parser->saveTuplesToDB($tuples);
     // Handles updating the objectives and related assessments
     foreach ($tuples as $tupleKey => $tuple) {
         // Save the learning_objective
         // Template for this was found in \mod\glossary\edit.php
         $learningObjectiveTypes = get_learning_objective_types();
         foreach ($learningObjectiveTypes as $learningObjectiveType) {
             $key = 'learning_objective_' . $learningObjectiveType;
             if (array_key_exists($key, $tuple) and is_array($tuple[$key])) {
                 foreach ($tuple[$key] as $objectiveId) {
                     $newLink = new stdClass();
                     $newLink->sessionid = $tuple['id'];
                     $newLink->objectiveid = $objectiveId;
                     $DB->insert_record('sessionobjectives', $newLink, false);
                 }
             }
         }
         // Save the assessments
         // Template for this was found in \mod\glossary\edit.php
         if (array_key_exists('assessments', $tuple) and is_array($tuple['assessments'])) {
             foreach ($tuple['assessments'] as $assessmentId) {
                 $newLink = new stdClass();
                 $newLink->sessionid = $tuple['id'];
                 $newLink->assessmentid = $assessmentId;
                 $DB->insert_record('session_related_assessment', $newLink, false);
             }
         }
         // Save the learning strategies
         // Template for this was found in \mod\glossary\edit.php
         if (array_key_exists('sessionteachingstrategy', $tuple) and is_array($tuple['sessionteachingstrategy'])) {
             $strategies = get_teaching_strategies();
             foreach ($tuple['sessionteachingstrategy'] as $strategyIndex) {
                 $newLink = new stdClass();
                 $newLink->sessionid = $tuple['id'];
                 $newLink->strategy = $strategies[$strategyIndex];
                 $DB->insert_record('sessionteachingstrategies', $newLink);
             }
         }
         // Save the topics
         // Template for this was found in \mod\glossary\edit.php
         if (array_key_exists('all_topics_text_array', $tuple) and !is_null($tuple['all_topics_text_array'])) {
             $topic_array = explode(session_form::TOPIC_SEPARATOR, $tuple['all_topics_text_array']);
             foreach ($topic_array as $topicname) {
                 $newLink = new stdClass();
                 $newLink->sessionid = $tuple['id'];
                 $newLink->topicname = $topicname;
                 $DB->insert_record('sessiontopics', $newLink, false);
             }
         }
     }
 }