Files
        @ 60bdc2d2972a
    
        
              Branch filter: 
        
    Location: SlatePermutate/class.section.php
        
            
            60bdc2d2972a
            11.7 KiB
            text/x-php
        
        
    
    Make the professor attribute of the Section class optionalish.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361  | <?php
//--------------------------------------------------
// class.section.php  Author:Nathan Gelderloos
//
// Represents a section of a class.
//--------------------------------------------------
   
class Section
{
  private $letter;	// Section letter
  private $prof;	// Professor
  private $start;	// Start time
  private $tend;	// End time
  private $idays;	// Integer version of meeting days
  private $bdays;	// Boolean array of meeting days
  /**
   * \brief
   *   Construct a Section.
   *
   * \param $letter
   *   The identifier (often a letter or numeral) of this section. For
   *   CS-262-A, this would be 'a'.
   * \param $time_start
   *   The time of day when this section meets. Formatted as a string,
   *   with the 24-hr representation of the hour taking the first two
   *   characters and a two-digit representation of minutes taking the
   *   next two characters.
   * \param $time_end
   *   The time of day when this section's meeting is over.
   * \param $days
   *   A string representing the days that this section meets. The
   *   format of this string is an ordered series of numerals less
   *   than or equal to 5. Each numeral from 1 through 5 represents
   *   one of Monday, Tuesday, Wednesday, Thursday, and Friday. For
   *   example, '135' would be for a course which meets on Monday,
   *   Wednesday, and Friday.
   * \param $prof
   *   The faculty person(s) who teaches this section.
   */
  function __construct ($letter, $time_start, $time_end, $days, $prof = '')
  {
    $this->letter = $letter;
    $this->start = $time_start;
    $this->tend = $time_end;
    $this->idays = $days;
    $this->bdays = $this->setbdays();
    $this->prof = $prof;
  }
  function setbdays()
  {
    $result = array(FALSE, FALSE, FALSE, FALSE, FALSE);
    if($this->idays == 12345)
      {$result[0] = true; $result[1] = true; $result[2] = true; $result[3] = true; $result[4] = true;}
    if($this->idays == 1234)
      {$result[0] = true; $result[1] = true; $result[2] = true; $result[3] = true; $result[4] = false;}
    if($this->idays == 1235)
      {$result[0] = true; $result[1] = true; $result[2] = true; $result[3] = false; $result[4] = true;}
    if($this->idays == 1245)
      {$result[0] = true; $result[1] = true; $result[2] = false; $result[3] = true; $result[4] = true;}
    if($this->idays == 1345)
      {$result[0] = true; $result[1] = false; $result[2] = true; $result[3] = true; $result[4] = true;}
    if($this->idays == 2345)
      {$result[0] = false; $result[1] = true; $result[2] = true; $result[3] = true; $result[4] = true;}
    if($this->idays == 123)
      {$result[0] = true; $result[1] = true; $result[2] = true; $result[3] = false; $result[4] = false;}
    if($this->idays == 124)
      {$result[0] = true; $result[1] = true; $result[2] = false; $result[3] = true; $result[4] = false;}
    if($this->idays == 125)
      {$result[0] = true; $result[1] = true; $result[2] = false; $result[3] = false; $result[4] = true;}
    if($this->idays == 134)
      {$result[0] = true; $result[1] = false; $result[2] = true; $result[3] = true; $result[4] = false;}
    if($this->idays == 135)
      {$result[0] = true; $result[1] = false; $result[2] = true; $result[3] = false; $result[4] = true;}
    if($this->idays == 145)
      {$result[0] = true; $result[1] = false; $result[2] = false; $result[3] = true; $result[4] = true;}
    if($this->idays == 234)
      {$result[0] = false; $result[1] = true; $result[2] = true; $result[3] = true; $result[4] = false;}
    if($this->idays == 235)
      {$result[0] = false; $result[1] = true; $result[2] = true; $result[3] = false; $result[4] = true;}
    if($this->idays == 245)
      {$result[0] = false; $result[1] = true; $result[2] = false; $result[3] = true; $result[4] = true;}
    if($this->idays == 345)
      {$result[0] = false; $result[1] = false; $result[2] = true; $result[3] = true; $result[4] = true;}
    if($this->idays == 12)
      {$result[0] = true; $result[1] = true; $result[2] = false; $result[3] = false; $result[4] = false;}
    if($this->idays == 13)
      {$result[0] = true; $result[1] = false; $result[2] = true; $result[3] = false; $result[4] = false;}
    if($this->idays == 14)
      {$result[0] = true; $result[1] = false; $result[2] = false; $result[3] = true; $result[4] = false;}
    if($this->idays == 15)
      {$result[0] = true; $result[1] = false; $result[2] = false; $result[3] = false; $result[4] = true;}
    if($this->idays == 23)
      {$result[0] = false; $result[1] = true; $result[2] = true; $result[3] = false; $result[4] = false;}
    if($this->idays == 24)
      {$result[0] = false; $result[1] = true; $result[2] = false; $result[3] = true; $result[4] = false;}
    if($this->idays == 25)
      {$result[0] = false; $result[1] = true; $result[2] = false; $result[3] = false; $result[4] = true;}
    if($this->idays == 34)
      {$result[0] = false; $result[1] = false; $result[2] = true; $result[3] = true; $result[4] = false;}
    if($this->idays == 35)
      {$result[0] = false; $result[1] = false; $result[2] = true; $result[3] = false; $result[4] = true;}
    if($this->idays == 45)
      {$result[0] = false; $result[1] = false; $result[2] = false; $result[3] = true; $result[4] = true;}
      
    if($this->idays == 1)
      {$result[0] = true; $result[1] = false; $result[2] = false; $result[3] = false; $result[4] = false;}
    if($this->idays == 2)
      {$result[0] = false; $result[1] = true; $result[2] = false; $result[3] = false; $result[4] = false;}
    if($this->idays == 3)
      {$result[0] = false; $result[1] = false; $result[2] = true; $result[3] = false; $result[4] = false;}
    if($this->idays == 4)
      {$result[0] = false; $result[1] = false; $result[2] = false; $result[3] = true; $result[4] = false;}
    if($this->idays == 5)
      {$result[0] = false; $result[1] = false; $result[2] = false; $result[3] = false; $result[4] = true;}
         
    return $result;
  }
  function getLetter()
  {
    return $this->letter;
  }
  function getProf()
  {
    return $this->prof;
  }
  function getStartTime()
  {
    return $this->start;
  }
  function getEndTime()
  {
    return $this->tend;
  }
  function getM()
  {
    return $this->bdays[0];
  }
  function getTu()
  {
    return $this->bdays[1];
  }
  function getW()
  {
    return $this->bdays[2];
  }
  function getTh()
  {
    return $this->bdays[3];
  }
  function getF()
  {
    return $this->bdays[4];
  }
  function getDay($i)
  {
    return $this->bdays[$i];
  }
  
  /**
   * \brief
   *   Create output suitable for editing on input.php.
   *
   * \see Classes::input_form_render()
   *
   * \param $class_key
   *   The same $class_key passed to Classes::input_form_render().
   * \param $section_key
   *   The index of this section.
   * \param $section_format
   *   The type of input method used for this section. Valid values
   *   are 'numerous', 'numbered', and 'lettered'
   */
  function input_form_render($class_key, $section_key, $section_format = 'numerous')
  {
    static $n = "\n";
    $out = '<tr class="section class' . $class_key . '">' . $n
      . '  <td class="none"></td>' . $n;
    switch ($section_format)
      {
      case 'numerous':
      default:
	/* see customIds() in scheduleInput.js */
	$out .= '  <td class="sectionIdentifier center">' . $n
	. '    <input type="text" size="1" class="required" title="Section Name"' . $n
	. '           name="postData[' . $class_key . '][' . $section_key . '][letter]"' . $n
	. '           value="' . htmlentities($this->letter) . '" />' . $n
	. "  </td>\n";
      break;
      }
    $out .= "  <td>\n"
      . '    <select class="selectRequired" name="postData[' . $class_key . '][' . $section_key . '][start]">' . $n;
    for ($h = 7; $h <= 21; $h ++)
      {
	$nm = 'p';
	$hr = $h;
	if ($h < 12)
	  $nm = 'a';
	elseif ($h > 12)
	  $hr -= 12;
	foreach (array('00', '30') as $m)
	  {
	    $val = $h . $m;
	    $selected = '';
	    if ($this->start == $val)
	      $selected = ' selected="selected"';
	    $label = $hr . ':' . $m . $nm . 'm';
	    $out .= '      <option value="' . $val . '"' . $selected . '>' . $label . '</option>' . $n;
	  }
      }
    $out .= "    </select>\n"
      . "  </td>\n";
    /* ugh, code duplication :-(  --binki commenting on his own code*/
    $out .= "  <td>\n"
      . '    <select class="selectRequired" name="postData[' . $class_key . '][' . $section_key . '][end]">' . $n;
    for ($h = 7; $h <= 21; $h ++)
      {
	$nm = 'p';
	$hr = $h;
	if ($h < 12)
	  $nm = 'a';
	elseif ($h > 12)
	  $hr -= 12;
	foreach (array('20', '50') as $m)
	  {
	    $val = $h . $m;
	    $selected = '';
	    if ($this->tend == $val)
	      $selected = ' selected="selected"';
	    $label = $hr . ':' . $m . $nm . 'm';
	    $out .= '      <option value="' . $val . '"' . $selected . '>' . $label . '</option>' . $n;
	  }
      }
    $out .= "    </select>\n"
      . "  </td>\n";
    foreach ($this->bdays as $day_key => $day_enabled)
      {
	if ($day_enabled)
	  $day_enabled = 'checked="checked"';
	else
	  $day_enabled = '';
	$out .= "  <td>\n"
	  . '    <input type="checkbox" class="daysRequired"'
	  . '           name="postData[' . $class_key . '][' . $section_key . '][days][' . $day_key . ']" value="1" ' . $day_enabled . ' />' . $n
	  . "  </td>\n";
      }
    $out .= '  <td><div class="deleteSection"><input type="button" value="X" class="gray" /></div></td>' . $n;
    $out .= '  <td></td>' . $n;
    $out .= "</tr>\n";
    return $out;
  }
  /**
   * \brief
   *   Splits up a section specifier into dept, course number, and
   *   section.
   *
   * For example, will return array('CS', '262', 'A') for 'CS-262-A'
   * or 'CS262A' or 'cs-262a'. This function is not for dealing with
   * course synonyms.
   *
   * \param $section_spec
   *   A string starting with a section specifier. If only the
   *   department is found, an array of size one is returned. If the
   *   course number is also found, both department and course id are
   *   returned. If all three are found, the array has three elements.
   *
   *   This array is keyed, so the found items may be referred to as
   *   'deptartment', 'course', and 'section'.
   *
   * \return
   *   An array with the department, course number, and section
   *   identifier. This array may be empty or have from one through
   *   three elements depending on the validity and precision of the
   *   $section_spec.
   */
  public static function parse($section_spec)
  {
    $ret = array();
    $section_spec = trim($section_spec);
    if (!preg_match(';([a-zA-Z]+)[^0-9]*;', $section_spec, $dept_matches))
      return $ret;
    /*
     * remove away the already-parsed stuff, including gunk between the
     * dept and the course num.
     */
    $section_spec = trim(substr($section_spec, strlen($dept_matches[0])));
    $ret['department'] = strtoupper($dept_matches[1]);
    if (!preg_match(';([0-9]+)[^a-zA-Z0-9]*;', $section_spec, $course_matches))
      return $ret;
    /* skip gunk */
    $section_spec = trim(substr($section_spec, strlen($course_matches[0])));
    $ret['course'] = $course_matches[1];
    /*
     * we accept _either_ alphabetic section _or_ numeric section (the
     * latter is for cedarville, particulaly)
     */
    if (!preg_match(';([0-9]+|[a-zA-Z]+);', $section_spec, $section_matches))
      return $ret;
    $ret['section'] = strtoupper($section_matches[1]);
    return $ret;
  }
  /**
   * \brief
   *   Get an array of information needed by the AJAX stuff.
   */
  public function to_json_array()
  {
    static $daymap = array(0 => 'm', 1 => 't', 2 => 'w', 3 => 'u', 4 => 'f');
    $json_array = array('section' => $this->letter,
			'prof' => $this->prof,
			'time_start' => $this->start,
			'time_end' => $this->tend,
			'days' => array(),
			);
    for ($day = 0; $day < 5; $day ++)
      $json_array['days'][$daymap[$day]] = $this->getDay($day);
    return $json_array;
  }
}
 |