/**
  * This method tests check_draft_id().  I test the two cases that can
  * exist: one where the ID is valid, one where it is not.
  *
  */
 public function test_check_draft_id()
 {
     // This ID exists.
     $fileid = $this->fileids[0];
     $this->assertTrue((bool) check_draft_id($fileid));
     // While this one does not.
     $fileid = get_unused_file_id();
     $this->assertFalse((bool) check_draft_id($fileid));
 }
/**
 * This function processes the submitted form for all of the sections.  Most of the work
 * is completed via using the section_icon class and its associated routines.  The data
 * is pulled from the form, a check is made to see if any boxes are ticked, then the
 * desired result is processed.  Any draft files that need to be saved are saved in this
 * function.
 *
 * TODO: some logic should be added to outright delete a settings record if it has neither
 * a disable flag or custom label associated with it.
 *
 */
function process_form($courseid, $blockid, &$submittedform, &$sectionheaders, $numberofsections)
{
    global $DB;
    for ($i = 0; $i < $numberofsections; $i++) {
        $filepickername = 'fileinfo_' . "{$i}";
        $hiddenidname = 'hiddenid_' . "{$i}";
        $masterupdatename = 'masterid_' . "{$i}";
        $deleteiconname = 'deleteid_' . "{$i}";
        $noiconname = 'noicon_' . "{$i}";
        $newcourseid = $courseid;
        $customlabelfield = 'customlabelfield_' . "{$i}";
        $customlabelcheckbox = 'customlbelcheckbox_' . "{$i}";
        $si = new section_icon($courseid, $sectionheaders[$i]);
        // Update master via using courseid 0.
        if (!isset($submittedform->{$masterupdatename})) {
            // Use courseid of 1 so we get the correct context.
            $newcourseid = 1;
            $context = context_course::instance($newcourseid);
        } else {
            // Save block context instead of the course context for course-specific records.
            $context = context_block::instance($blockid);
        }
        // Check for delete first.
        if (isset($submittedform->{$deleteiconname})) {
            $si->delete_record();
            continue;
        }
        $draftitemid = file_get_submitted_draft_itemid($filepickername);
        $itemid = get_unused_file_id();
        file_save_draft_area_files($draftitemid, $context->id, BNN_BLOCK_SAVE_COMPONENT, BNN_BLOCK_SAVE_AREA, $itemid, array('subdirs' => 0, 'maxbytes' => BNN_MAX_BYTES, 'maxfiles' => BNN_MAX_FILES));
        // Only update if the user uploaded a file.
        if (check_draft_id($draftitemid)) {
            $si->update_icon_record($newcourseid, $itemid);
        }
        // The other IFs are separate cases because the user can do multiple things at once.
        if ($submittedform->{$noiconname} != $si->get_icon_disable()) {
            $si->update_disableicon($submittedform->{$noiconname});
        }
        if ($submittedform->{$customlabelcheckbox} == true) {
            // Only write label if different from existing label.
            if ($si->get_custom_label() != $submittedform->{$customlabelfield}) {
                $si->update_label($submittedform->{$customlabelfield});
            }
        } else {
            // Only write a null if the record exists and is not already null.
            if ($si->settings_exists() && $si->get_custom_label() != null) {
                $si->update_label(null);
            }
        }
    }
}
 /**
  * This function creates the various files that are used with testing.  All of the files are
  * created as text files, even though the ones used in a course would be images.  With respect
  * to the plugin, however, it doesn't care too much about what the file type is (pluginfile.php
  * will serve up anything).
  *
  */
 protected function create_files()
 {
     if ($this->blockid == null) {
         echo 'Please create block prior to creating files.  Context may be invalid.';
         return;
     }
     // Sequence of course IDs for saving the files, 1 => global icon.
     $ids = array(1, $this->blockid, 1, $this->blockid, 1, $this->blockid, 1, 1);
     $fs = get_file_storage();
     for ($i = 0; $i < self::TOTAL_FILES; $i++) {
         $filename = 'testfile' . $i . '.txt';
         $itemid = get_unused_file_id();
         $contextid = $ids[$i] == 1 ? context_course::instance($ids[$i])->id : context_block::instance($ids[$i])->id;
         $fileinfo = array('contextid' => $contextid, 'component' => BNN_BLOCK_SAVE_COMPONENT, 'filearea' => BNN_BLOCK_SAVE_AREA, 'itemid' => $itemid, 'filepath' => '/', 'filename' => $filename);
         $fs->create_file_from_string($fileinfo, $this->filecontents[$i]);
         // Store ID for later use.
         $this->fileids[] = $itemid;
     }
 }