/**
  * Form definition: creates the form by creating a new segment for every section.  Each
  * segment contains the ID, section header, a picture of the old image, the filemanager
  * element, and two checkboxes for controls.  I also store the blockid and courseid at
  * the bottom of the page so that posting works properly.
  *
  */
 public function definition()
 {
     global $DB;
     $mform =& $this->_form;
     for ($i = 0; $i < $this->numberofsections; $i++) {
         $headername = 'displayinfo_' . "{$i}";
         $filepickername = 'fileinfo_' . "{$i}";
         $hiddenid = 'hiddenid_' . "{$i}";
         $masterupdatename = 'masterid_' . "{$i}";
         $deleteiconname = 'deleteid_' . "{$i}";
         $noiconname = 'noicon_' . "{$i}";
         $customlabelfield = 'customlabelfield_' . "{$i}";
         $customlabelcheckbox = 'customlbelcheckbox_' . "{$i}";
         $sectionname = $this->sectionheaders[$i];
         $courseid = $this->courseid;
         $si = new section_icon($courseid, $sectionname);
         $imagefile = $si->get_image(true);
         $mform->addElement('hidden', $hiddenid, $si->get_id());
         $mform->setType($hiddenid, PARAM_INT);
         $mform->addElement('header', $headername, $this->sectionheaders[$i]);
         $label = get_string('existingimage', BNN_LANG_TABLE);
         $mform->addElement('html', "<i>{$label}</i> <img alt='blank' src='{$imagefile}' height='35' width='50'/>");
         $mform->addElement('filemanager', $filepickername, get_string('newimage', BNN_LANG_TABLE), null, array('subdirs' => 0, 'maxbytes' => BNN_MAX_BYTES, 'maxfiles' => BNN_MAX_FILES, 'accepted_types' => 'image'));
         // Icon settings.
         $pagegroup = array();
         $pagegroup[] = $mform->createElement('checkbox', $deleteiconname, '', get_string('deleteicon', BNN_LANG_TABLE));
         $pagegroup[] = $mform->createElement('advcheckbox', $noiconname, '', get_string('noicon', BNN_LANG_TABLE), null, array(0, 1));
         // Can only disable if not deleting.
         $mform->disabledIf($noiconname, $deleteiconname, 'checked');
         $pagegroup[] = $mform->createElement('checkbox', $masterupdatename, '', get_string('updatecourseonly', BNN_LANG_TABLE));
         // Can only update a record if not deleting/disabling.
         $mform->disabledIf($masterupdatename, $deleteiconname, 'checked');
         $mform->disabledIf($masterupdatename, $noiconname, 'checked');
         $mform->addGroup($pagegroup, 'deletebar', get_string('iconsettings', BNN_LANG_TABLE), null, false);
         // Custom section text.
         $pagegroup = array();
         $pagegroup[] = $mform->createElement('text', $customlabelfield, '', array());
         $mform->setType($customlabelfield, PARAM_TEXT);
         $pagegroup[] = $mform->createElement('advcheckbox', $customlabelcheckbox, '', get_string('customlabel', BNN_LANG_TABLE), null, array(0, 1));
         $mform->disabledIf($customlabelfield, $customlabelcheckbox);
         // Custom text must be selected.
         $mform->disabledIf($customlabelfield, $deleteiconname, 'checked');
         // Invalid when deleting/disabling.
         $mform->disabledIf($customlabelfield, $noiconname, 'checked');
         $mform->disabledIf($customlabelcheckbox, $deleteiconname, 'checked');
         // Invalid when deleting/disabling.
         $mform->disabledIf($customlabelcheckbox, $noiconname, 'checked');
         $mform->addGroup($pagegroup, 'customtext', '', null, false);
     }
     // Hidden elements (courseid + blockid: needed for posting).
     $mform->addElement('hidden', 'courseid');
     $mform->setType('courseid', PARAM_INT);
     $mform->addElement('hidden', 'blockid');
     $mform->setType('blockid', PARAM_INT);
     $this->add_action_buttons();
 }
/**
 * 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 method tests get_image() for both the master record and course-specific
  * records.  I also check to ensure an empty string is returned when the record
  * does not exist.
  *
  */
 public function test_get_image()
 {
     $courseid = $this->testcourseid;
     $sectionname = "Craig's Resources";
     $si = new section_icon($courseid, $sectionname);
     // Test for course specific image -> returns course specific image.
     $image = $si->get_image(false);
     $context = context_block::instance($this->blockid);
     $pos = strpos($image, 'pluginfile.php/' . $context->id . '/block_nurs_navigation/nursing_image/' . $this->fileids[self::SECTION1_COURSE_FILE_ID_INDEX] . '/testfile1.txt');
     $this->assertTrue((bool) ($pos > 0));
     // Test for course specific while allowing master -> still returns course specific first.
     $image = $si->get_image(true);
     $pos = strpos($image, 'pluginfile.php/' . $context->id . '/block_nurs_navigation/nursing_image/' . $this->fileids[self::SECTION1_COURSE_FILE_ID_INDEX] . '/testfile1.txt');
     $this->assertTrue((bool) ($pos > 0));
     // Flip to record with only master image.
     $sectionname = "Topic 4";
     $si = new section_icon($courseid, $sectionname);
     // Test for course specific -> no record returned.
     $image = $si->get_image(false);
     $this->assertEquals($image, '');
     // Test for master -> master returned.
     $image = $si->get_image(true);
     $context = context_course::instance(1);
     $pos = strpos($image, 'pluginfile.php/' . $context->id . '/block_nurs_navigation/nursing_image/' . $this->fileids[self::SECTION4_GLOBAL_FILE_ID_INDEX] . '/testfile6.txt');
     $this->assertTrue((bool) ($pos > 0));
     // Flip to record with neither master nor course-specific: make sure empty strings are returned.
     $sectionname = "This topic does not exist";
     $si = new section_icon($courseid, $sectionname);
     // Course specific.
     $image = $si->get_image(false);
     $this->assertEquals($image, '');
     // Master check.
     $image = $si->get_image(true);
     $this->assertEquals($image, '');
 }
 /**
  *
  * This function formats and returns a link to the image for a particular section and
  * course.  If a particular section is active, the icons for other sections are faded
  * out.
  *
  */
 private function get_block_icon_link($courseid, $sectionnumber, $sectionheader, $currentsection, $fontsize)
 {
     if (!$this->verify_visibility($sectionheader)) {
         return;
     }
     $si = new section_icon($courseid, $sectionheader);
     // If icon is set to disable, then skip.
     if ($si->get_icon_disable()) {
         return;
     }
     // Check for custom label text.
     $customlabel = $si->get_custom_label();
     $sectionheader = $customlabel != null ? $customlabel : $sectionheader;
     $imagefile = $si->get_image(true);
     $sectionnumberformatted = $sectionnumber + 1;
     // Grab height/width from admin settings.
     $height = get_config('nurs_navigation', 'Image_Height');
     $width = get_config('nurs_navigation', 'Image_Width');
     $outputbuffer = "<li style='display: inline-block; vertical-align: top; width: 100%; margin-bottom: 10px;\n                         padding: 0; list-style-type: none;'><span style='font-size: {$fontsize};'>";
     $outputbuffer .= "<a title=\"Section: {$sectionheader}\n                          \"href='/course/view.php?id={$courseid}&section={$sectionnumberformatted}'>";
     $outputbuffer .= "<img alt=\"{$sectionheader}\" src='{$imagefile}' height='{$height}' width='{$width}' ";
     if ($currentsection != 0 && $sectionnumberformatted == $currentsection) {
         $outputbuffer .= "class=\"faded\"";
     }
     $outputbuffer .= "/>";
     if (!isset($this->config->disabletext) || isset($this->config->disabletext) && !$this->config->disabletext) {
         $outputbuffer .= "<br />{$sectionheader}";
     }
     $outputbuffer .= "</a></span></li>";
     return $outputbuffer;
 }