Newer
Older
moodlog_web / habit.php
0xRoM on 4 Jan 2024 28 KB minor logic error
  1. <?php
  2.  
  3. function habit_update($word, $moodlog) {
  4. // Call habit_check_daily function
  5. $result = habit_check_daily($word, $moodlog);
  6.  
  7. // Get today's date
  8. $todayDate = date("Y-m-d");
  9.  
  10. // Read the existing mood log file
  11. $lines = file($moodlog);
  12.  
  13. // Check if habit_check_daily returns true
  14. if ($result) {
  15. // Iterate through each line in the file
  16. foreach ($lines as $key => $line) {
  17. // Extract the date from the line
  18. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match);
  19.  
  20. if (!empty($match[1])) {
  21. $lineDate = $match[1];
  22.  
  23. // Check if the line date is today
  24. if ($lineDate === $todayDate) {
  25. // Remove the word along with the '#' symbol (case-insensitive)
  26. $modifiedLine = preg_replace("/#?$word\b/i", '', $line);
  27.  
  28. // Remove the whole line if there is nothing after the number
  29. if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2} \[\d+\]\s*$/', $modifiedLine)) {
  30. unset($lines[$key]);
  31. } else {
  32. $lines[$key] = $modifiedLine;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. // Save the modified lines back to the file
  39. file_put_contents($moodlog, implode("", $lines));
  40. } else {
  41. $foundToday = false;
  42.  
  43. // Check if there is a line with today's date
  44. foreach ($lines as $key => $line) {
  45. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $match);
  46.  
  47. if (!empty($match[1]) && $match[1] === $todayDate) {
  48. $foundToday = true;
  49.  
  50. // Remove the trailing newline
  51. $lines[$key] = rtrim($line);
  52.  
  53. // Append #$text to the line
  54. $lines[$key] .= " #$word";
  55.  
  56. // Re-add the newline
  57. $lines[$key] .= "\n";
  58.  
  59. break;
  60. }
  61. }
  62.  
  63. // If today's date is not found, prepend a new line
  64. if (!$foundToday) {
  65. // Prepend a new line with Y-m-d hh:mm [0] #$word
  66. $newLine = $todayDate . " " . date("H:i") . " [0] #$word\n";
  67. array_unshift($lines, $newLine);
  68. }
  69.  
  70. // Save the modified lines back to the file
  71. file_put_contents($moodlog, implode("", $lines));
  72. }
  73. }
  74.  
  75. function habit_cal_daily($filter){
  76. global $moodlog;
  77.  
  78. $fileContent = file_get_contents($moodlog);
  79. $lines = explode("\n", $fileContent);
  80. $dateOccurrences = [];
  81.  
  82. // Get the current date
  83. $currentDate = date('Y-m-d');
  84.  
  85. foreach ($lines as $line) {
  86. // Extract the date from the line
  87. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  88. $date = isset($matches[0]) ? $matches[0] : null;
  89.  
  90. // Check if the line contains the specified word as a hashtag
  91. $wordFound = (strpos($line, "#$filter") !== false);
  92.  
  93. // Populate the dateOccurrences array
  94. if ($date !== null) {
  95. // Calculate the difference in weeks
  96. $weekDifference = floor((strtotime($currentDate) - strtotime($date)) / (7 * 24 * 60 * 60));
  97.  
  98. // Check if the week is more than 24 weeks ago
  99. if ($weekDifference > 24) {
  100. break;
  101. }
  102.  
  103. if (!isset($dateOccurrences[$date])) {
  104. $dateOccurrences[$date] = 0;
  105. }
  106.  
  107. if ($wordFound) {
  108. $dateOccurrences[$date] = 1;
  109. }
  110. }
  111. }
  112.  
  113. // Remove entries with a value of 0
  114. $dateOccurrences = array_filter($dateOccurrences, function ($value) {
  115. return $value !== 0;
  116. });
  117.  
  118. // Convert keys to timestamps and print in a simple JSON format with surrounding {}
  119. $result = '{';
  120. foreach ($dateOccurrences as $date => $value) {
  121. $timestamp = strtotime($date);
  122. $result .= "\"$timestamp\":$value,";
  123. }
  124.  
  125. // Remove trailing comma
  126. $result = rtrim($result, ',');
  127.  
  128. $result .= '}';
  129.  
  130. echo $result;
  131. }
  132.  
  133. function habit_cal_weekly($filter){
  134. global $moodlog;
  135.  
  136. $fileContent = file_get_contents($moodlog);
  137. $lines = explode("\n", $fileContent);
  138. $dateOccurrences = [];
  139. $weeklyOccurrences = [];
  140.  
  141. foreach ($lines as $line) {
  142. // Extract the date from the line
  143. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  144. $date = isset($matches[0]) ? $matches[0] : null;
  145.  
  146. // Explode the line into words
  147. $words = explode(' ', $line);
  148.  
  149. // Check if the line contains the specified word as a hashtag
  150. $wordFound = false;
  151. foreach ($words as $wordInLine) {
  152. // Check if the word contains "#takeout" (case-insensitive)
  153. if (stripos($wordInLine, "#$filter") !== false) {
  154. $wordFound = true;
  155. break;
  156. }
  157. }
  158.  
  159. // Populate the dateOccurrences array
  160. if (!isset($dateOccurrences[$date])) {
  161. $dateOccurrences[$date] = 0;
  162. }
  163. if ($date !== null && $wordFound) {
  164. $dateOccurrences[$date]++;
  165. }
  166.  
  167. // Output the line whether the word is found or not
  168. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  169. }
  170.  
  171. foreach ($dateOccurrences as $date => $count) {
  172. // Convert each date to the format $year-$weekNo
  173. $dateTime = new DateTime($date);
  174. $yearWeek = $dateTime->format('Y-m-W');
  175.  
  176. // Add +1 for each date converted into $year-$weekNo as a key
  177. if (isset($weeklyOccurrences[$yearWeek])) {
  178. if( $dateOccurrences[$date] == 1)
  179. $weeklyOccurrences[$yearWeek]++;
  180. } else {
  181. if($dateOccurrences[$date] == 1){
  182. $weeklyOccurrences[$yearWeek] = 1;
  183. }else{
  184. $weeklyOccurrences[$yearWeek] = 0;
  185. }
  186. }
  187. }
  188.  
  189. // Remove entries with a value of 0
  190. $weeklyOccurrences = array_filter($weeklyOccurrences, function ($value) {
  191. return $value !== 0;
  192. });
  193. //print_r($weeklyOccurrences);
  194.  
  195. $result = '{';
  196. foreach ($weeklyOccurrences as $date => $value) {
  197. $result .= "\"$date\":$value,";
  198. }
  199. // Remove trailing comma
  200. $result = rtrim($result, ',');
  201. $result .= '}';
  202. echo $result;
  203. }
  204.  
  205. function habit_parse_file($fileContent) {
  206. $flattenedArray = array();
  207.  
  208. // Split the file content into lines
  209. $lines = explode("\n", $fileContent);
  210.  
  211. foreach ($lines as $line) {
  212. // Use regular expressions to extract relevant information
  213. preg_match('/\[([^\]]+)\]\[([^\]]+)\] (.+) - (.+)/', $line, $matches);
  214.  
  215. if (count($matches) == 5) {
  216. $category = $matches[1];
  217. $value = $matches[2];
  218. $activity = $matches[3];
  219. $description = $matches[4];
  220.  
  221. // Add to the flattened array
  222. $flattenedArray[] = array(
  223. 'Category' => $category,
  224. 'Value' => $value,
  225. 'Activity' => $activity,
  226. 'Description' => $description
  227. );
  228. }
  229. }
  230.  
  231. return $flattenedArray;
  232. }
  233.  
  234. function habit_check_daily($word, $filename) {
  235. // Split the file content into an array of lines
  236. $fileContent = file_get_contents($filename);
  237. $lines = explode("\n", $fileContent);
  238. $dateOccurrences = [];
  239.  
  240. foreach ($lines as $line) {
  241. // Extract the date from the line
  242. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  243. $date = isset($matches[0]) ? $matches[0] : null;
  244.  
  245. // Check if the line contains the specified word as a hashtag
  246. $wordFound = (strpos($line, "#$word") !== false);
  247.  
  248. // Populate the dateOccurrences array
  249. if ($date !== null) {
  250. if (!isset($dateOccurrences[$date])) {
  251. $dateOccurrences[$date] = 0;
  252. }
  253. if($wordFound){
  254. $dateOccurrences[$date] = 1;
  255. }
  256. }
  257. }
  258.  
  259. // Check streak
  260. $currentStreak = 0;
  261. $today = date('Y-m-d');
  262. //print_r($dateOccurrences);
  263.  
  264. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  265. $currentStreak++;
  266. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  267. }
  268.  
  269. if( $currentStreak >= 1){
  270. return true;
  271. }else{
  272. return false;
  273. }
  274. }
  275.  
  276. function habit_count_weekly($word, $filename) {
  277. $fileContent = file_get_contents($filename);
  278. $lines = explode("\n", $fileContent);
  279. $currentWeekNumber = (int)date("W");
  280. $wordCount = 0;
  281.  
  282. foreach ($lines as $line) {
  283. preg_match('/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/', $line, $matches);
  284.  
  285. if (!empty($matches)) {
  286. $lineDate = new DateTime($matches[1]);
  287. $lineWeekNumber = (int)$lineDate->format('W');
  288.  
  289. // Check if the line's week number is the same as the current week number
  290. if ($lineWeekNumber == $currentWeekNumber) {
  291. $wordCount += substr_count($line, "#$word");
  292. }
  293. }
  294. }
  295.  
  296. return $wordCount;
  297. }
  298.  
  299. function habit_get_stat_streak($word, $filename) {
  300. // Split the file content into an array of lines
  301. $fileContent = file_get_contents($filename);
  302. $lines = explode("\n", $fileContent);
  303. $dateOccurrences = [];
  304.  
  305. foreach ($lines as $line) {
  306. // Extract the date from the line
  307. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  308. $date = isset($matches[0]) ? $matches[0] : null;
  309.  
  310. // Check if the line contains the specified word as a hashtag
  311. $wordFound = (strpos($line, "#$word") !== false);
  312.  
  313. // Populate the dateOccurrences array
  314. if ($date !== null) {
  315. if (!isset($dateOccurrences[$date])) {
  316. $dateOccurrences[$date] = 0;
  317. }
  318. if($wordFound){
  319. $dateOccurrences[$date] = 1;
  320. }
  321. }
  322. }
  323.  
  324. // Check streak
  325. $currentStreak = 0;
  326. $today = date('Y-m-d');
  327.  
  328. while (isset($dateOccurrences[$today]) && $dateOccurrences[$today] > 0) {
  329. $currentStreak++;
  330. $today = date('Y-m-d', strtotime($today . ' - 1 day'));
  331. }
  332.  
  333. return $currentStreak;
  334. }
  335.  
  336. function habit_get_stat_streak_week($word, $multiplier, $filename) {
  337. // Split the file content into an array of lines
  338. $fileContent = file_get_contents($filename);
  339. $lines = explode("\n", $fileContent);
  340. $dateOccurrences = [];
  341. $weeklyOccurrences = [];
  342.  
  343. foreach ($lines as $line) {
  344. // Extract the date from the line
  345. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  346. $date = isset($matches[0]) ? $matches[0] : null;
  347.  
  348. // Explode the line into words
  349. $words = explode(' ', $line);
  350.  
  351. // Check if the line contains the specified word as a hashtag
  352. $wordFound = false;
  353. foreach ($words as $wordInLine) {
  354. // Check if the word contains "#takeout" (case-insensitive)
  355. if (stripos($wordInLine, "#$word") !== false) {
  356. $wordFound = true;
  357. break;
  358. }
  359. }
  360.  
  361. // Populate the dateOccurrences array
  362. if ($date !== null && $wordFound) {
  363. if (!isset($dateOccurrences[$date])) {
  364. $dateOccurrences[$date] = 0;
  365. }
  366. $dateOccurrences[$date]++;
  367. }
  368.  
  369. // Output the line whether the word is found or not
  370. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  371. }
  372.  
  373. // Output the occurrences
  374. //print_r($dateOccurrences);
  375.  
  376. // Create weekly occurrences array
  377.  
  378. foreach ($dateOccurrences as $date => $count) {
  379. // Convert each date to the format $year-$weekNo
  380. $dateTime = new DateTime($date);
  381. $yearWeek = $dateTime->format('Y-W');
  382.  
  383. // Add +1 for each date converted into $year-$weekNo as a key
  384. if (isset($weeklyOccurrences[$yearWeek])) {
  385. $weeklyOccurrences[$yearWeek]++;
  386. } else {
  387. $weeklyOccurrences[$yearWeek] = 1;
  388. }
  389. }
  390.  
  391. $streak = 0;
  392. $thisWeek = date('Y-W'); // Get the current week
  393.  
  394. while (isset($weeklyOccurrences[$thisWeek]) && $weeklyOccurrences[$thisWeek] >= $multiplier) {
  395. $streak++;
  396. $thisWeek = date('Y-W', strtotime($thisWeek . ' -1 week'));
  397. }
  398.  
  399. //print_r($dateOccurrences);
  400. //print_r($weeklyOccurrences);
  401.  
  402. return $streak;
  403. }
  404.  
  405. function habit_get_stat_missed($word, $filename) {
  406. // Split the file content into an array of lines
  407. $fileContent = file_get_contents($filename);
  408. $lines = explode("\n", $fileContent);
  409.  
  410. // Set variables
  411. $missedDays = 0;
  412. $currentDate = date('Y-m-d');
  413. $wordFound = false;
  414.  
  415. // Find the minimum date in the lines
  416. $minDate = null;
  417. foreach ($lines as $line) {
  418. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  419. $date = isset($matches[0]) ? $matches[0] : null;
  420.  
  421. if ($date !== null && ($minDate === null || $date < $minDate)) {
  422. $minDate = $date;
  423. }
  424. }
  425.  
  426. // Loop for each day
  427. while (true) {
  428. // Check if the word is found on any line for the current date
  429. foreach ($lines as $line) {
  430. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  431. $date = isset($matches[0]) ? $matches[0] : null;
  432.  
  433. $wordFound = $wordFound || ($date === $currentDate && strpos($line, "#$word") !== false);
  434. }
  435.  
  436. // If the word is found or we reach the beginning of the available dates, break the loop
  437. if ($wordFound || $currentDate < $minDate) {
  438. break;
  439. }
  440.  
  441. // Move to the previous day
  442. $missedDays++;
  443. $currentDate = date('Y-m-d', strtotime($currentDate . ' - 1 day'));
  444. }
  445.  
  446. return $wordFound ? $missedDays : 0;
  447. }
  448.  
  449. function habit_get_stat_missed_week($word, $multiplier, $filename) {
  450. // Split the file content into an array of lines
  451. $fileContent = file_get_contents($filename);
  452. $lines = explode("\n", $fileContent);
  453. $dateOccurrences = [];
  454. $weeklyOccurrences = [];
  455.  
  456. foreach ($lines as $line) {
  457. // Extract the date from the line
  458. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  459. $date = isset($matches[0]) ? $matches[0] : null;
  460.  
  461. // Explode the line into words
  462. $words = explode(' ', $line);
  463.  
  464. // Check if the line contains the specified word as a hashtag
  465. $wordFound = false;
  466. foreach ($words as $wordInLine) {
  467. // Check if the word contains "#takeout" (case-insensitive)
  468. if (stripos($wordInLine, "#$word") !== false) {
  469. $wordFound = true;
  470. break;
  471. }
  472. }
  473.  
  474. // Populate the dateOccurrences array
  475. if (!isset($dateOccurrences[$date])) {
  476. $dateOccurrences[$date] = 0;
  477. }
  478. if ($date !== null && $wordFound) {
  479. $dateOccurrences[$date]++;
  480. }
  481.  
  482. // Output the line whether the word is found or not
  483. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  484. }
  485.  
  486. foreach ($dateOccurrences as $date => $count) {
  487. // Convert each date to the format $year-$weekNo
  488. $dateTime = new DateTime($date);
  489. $yearWeek = $dateTime->format('Y-W');
  490.  
  491. // Add +1 for each date converted into $year-$weekNo as a key
  492. if (isset($weeklyOccurrences[$yearWeek])) {
  493. if( $dateOccurrences[$date] == 1)
  494. $weeklyOccurrences[$yearWeek]++;
  495. } else {
  496. if($dateOccurrences[$date] == 1){
  497. $weeklyOccurrences[$yearWeek] = 1;
  498. }else{
  499. $weeklyOccurrences[$yearWeek] = 0;
  500. }
  501. }
  502. }
  503.  
  504. //print_r($dateOccurrences);
  505. //print_r($weeklyOccurrences);
  506.  
  507. $countZeros = 0;
  508.  
  509. foreach ($weeklyOccurrences as $value) {
  510. if ($value < $multiplier) {
  511. $countZeros++;
  512. } else {
  513. break; // Stop counting when a non-zero value is encountered
  514. }
  515. }
  516. return $countZeros;
  517. }
  518.  
  519. function habit_get_stat_top($word, $filename) {
  520. $fileContent = file_get_contents($filename);
  521.  
  522. // Split the file content into an array of lines
  523. $lines = explode("\n", $fileContent);
  524.  
  525. $dateOccurrences = [];
  526. $currentStreak = 0;
  527. $longestStreak = 0;
  528.  
  529. foreach ($lines as $line) {
  530. // Extract the date from the line
  531. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  532. $date = isset($matches[0]) ? $matches[0] : null;
  533.  
  534. // Check if the line contains the specified word as a hashtag
  535. $wordFound = (strpos($line, "#$word") !== false);
  536.  
  537. // Populate the dateOccurrences array
  538. if ($date !== null) {
  539. if (!isset($dateOccurrences[$date])) {
  540. $dateOccurrences[$date] = 0;
  541. }
  542. if($wordFound){
  543. $dateOccurrences[$date] = 1;
  544. }
  545. }
  546. }
  547.  
  548. foreach ($dateOccurrences as $date => $value) {
  549. if ($value == 1) {
  550. $currentStreak++;
  551. } else {
  552. $currentStreak = 0;
  553. }
  554.  
  555. // Update the longest streak if the current streak is greater
  556. $longestStreak = max($longestStreak, $currentStreak);
  557. }
  558.  
  559. //print_r($dateOccurrences);
  560. return $longestStreak;
  561. }
  562.  
  563. function habit_get_stat_top_week($word, $multiplier, $filename) {
  564. // Split the file content into an array of lines
  565. $fileContent = file_get_contents($filename);
  566. $lines = explode("\n", $fileContent);
  567. $dateOccurrences = [];
  568. $weeklyOccurrences = [];
  569.  
  570. foreach ($lines as $line) {
  571. // Extract the date from the line
  572. preg_match('/(\b\d{4}-\d{2}-\d{2}\b)/', $line, $matches);
  573. $date = isset($matches[0]) ? $matches[0] : null;
  574.  
  575. // Explode the line into words
  576. $words = explode(' ', $line);
  577.  
  578. // Check if the line contains the specified word as a hashtag
  579. $wordFound = false;
  580. foreach ($words as $wordInLine) {
  581. // Check if the word contains "#takeout" (case-insensitive)
  582. if (stripos($wordInLine, "#$word") !== false) {
  583. $wordFound = true;
  584. break;
  585. }
  586. }
  587.  
  588. // Populate the dateOccurrences array
  589. if (!isset($dateOccurrences[$date])) {
  590. $dateOccurrences[$date] = 0;
  591. }
  592. if ($date !== null && $wordFound) {
  593. $dateOccurrences[$date]++;
  594. }
  595.  
  596. // Output the line whether the word is found or not
  597. //echo ($wordFound) ? "+ found: $line\n" : $line . "\n";
  598. }
  599.  
  600. foreach ($dateOccurrences as $date => $count) {
  601. // Convert each date to the format $year-$weekNo
  602. $dateTime = new DateTime($date);
  603. $yearWeek = $dateTime->format('Y-W');
  604.  
  605. // Add +1 for each date converted into $year-$weekNo as a key
  606. if (isset($weeklyOccurrences[$yearWeek])) {
  607. if( $dateOccurrences[$date] == 1)
  608. $weeklyOccurrences[$yearWeek]++;
  609. } else {
  610. if($dateOccurrences[$date] == 1){
  611. $weeklyOccurrences[$yearWeek] = 1;
  612. }else{
  613. $weeklyOccurrences[$yearWeek] = 0;
  614. }
  615. }
  616. }
  617.  
  618. //print_r($dateOccurrences);
  619. //print_r($weeklyOccurrences);
  620.  
  621. $currentStreak = 0;
  622. $longestStreak = 0;
  623.  
  624. foreach ($weeklyOccurrences as $week => $value) {
  625. if ($value >= $multiplier) {
  626. // Increment the streak length if the value is greater than or equal to the multiplier
  627. $currentStreak++;
  628. } else {
  629. // Reset the streak length if the value is less than the multiplier
  630. $currentStreak = 0;
  631. }
  632.  
  633. // Update the longest streak if the current streak is longer
  634. $longestStreak = max($longestStreak, $currentStreak);
  635. }
  636. return $longestStreak;
  637. }
  638.  
  639. function habit_get_stat_top_missing($word, $filename) {
  640. $fileContent = file_get_contents($filename);
  641.  
  642. // Split the file content into an array of lines
  643. $lines = explode("\n", $fileContent);
  644.  
  645. // Find the earliest instance of the word
  646. $earliestDateWithWord = null;
  647. foreach ($lines as $line) {
  648. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  649. $date = isset($matches[0]) ? $matches[0] : null;
  650.  
  651. // Check if the line contains the specified word as a hashtag
  652. $wordFound = (strpos($line, "#$word") !== false);
  653.  
  654. if ($wordFound && ($earliestDateWithWord === null || $date < $earliestDateWithWord)) {
  655. $earliestDateWithWord = $date;
  656. }
  657. }
  658.  
  659. if ($earliestDateWithWord === null) {
  660. // No instance of the word found, return 0
  661. return 0;
  662. }
  663.  
  664. $currentDate = $earliestDateWithWord;
  665. $today = date('Y-m-d');
  666. $dateArray = [];
  667.  
  668. // Initialize date array with 0 values starting from the earliest date with the word
  669. while ($currentDate <= $today) {
  670. $dateArray[$currentDate] = 0;
  671. $currentDate = date('Y-m-d', strtotime($currentDate . ' + 1 day'));
  672. }
  673.  
  674. // Loop over the file and update dateArray
  675. foreach ($lines as $line) {
  676. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  677. $date = isset($matches[0]) ? $matches[0] : null;
  678.  
  679. // Check if the line contains the specified word as a hashtag
  680. $wordFound = (strpos($line, "#$word") !== false);
  681.  
  682. if ($date !== null && isset($dateArray[$date])) {
  683. // Update dateArray based on word occurrence
  684. if($wordFound)
  685. $dateArray[$date] = 1;
  686. }
  687. }
  688.  
  689. // Find the longest streak of 0's in dateArray
  690. $currentStreak = 0;
  691. $longestStreak = 0;
  692.  
  693. foreach ($dateArray as $value) {
  694. if ($value === 0) {
  695. $currentStreak++;
  696. } else {
  697. $longestStreak = max($longestStreak, $currentStreak);
  698. $currentStreak = 0;
  699. }
  700. }
  701.  
  702. return $longestStreak;
  703. }
  704.  
  705. function habit_get_stat_year($word, $filename) {
  706. $fileContent = file_get_contents($filename);
  707.  
  708. // Split the file content into an array of lines
  709. $lines = explode("\n", $fileContent);
  710.  
  711. $daysWithWord = [];
  712.  
  713. foreach ($lines as $line) {
  714. // Extract the date from the line
  715. preg_match('/(\d{4}-\d{2}-\d{2})/', $line, $matches);
  716. $date = isset($matches[0]) ? $matches[0] : null;
  717.  
  718. // Check if the line contains the specified word as a hashtag
  719. $wordFound = (strpos($line, "#$word") !== false);
  720.  
  721. if ($date !== null && date('Y', strtotime($date)) == date('Y')) {
  722. // Check if the date is in the current year
  723. if (!isset($daysWithWord[$date])) {
  724. $daysWithWord[$date] = $wordFound ? 1 : 0;
  725. } else {
  726. $daysWithWord[$date] += $wordFound ? 1 : 0;
  727. }
  728. }
  729. }
  730.  
  731. $daysCount = array_sum($daysWithWord);
  732.  
  733. return $daysCount;
  734. }
  735.  
  736. function habit_get_json($word, $moodlog, $habitlog){
  737. $fileContent = file_get_contents($habitlog);
  738. $resultArray = habit_parse_file($fileContent);
  739. $found = false;
  740.  
  741. foreach ($resultArray as $section) {
  742. $jsonArray = array();
  743. $jsonArray['align'] = "";
  744. $jsonArray['buttonClass'] = "";
  745. $jsonArray['buttonText'] = "";
  746. if($section['Category'][0] == "d" && $section['Activity'] == $word){
  747. $found = true;
  748. $jsonArray['freq'] = "d";
  749.  
  750. if($section['Category'][1] == "+"){
  751. $jsonArray['align'] = "good";
  752. $jsonArray['calCol'] = "#58e81b";
  753. $jsonArray['title1'] = "Streak";
  754. $jsonArray['title2'] = "Missed";
  755. $jsonArray['title3'] = "Top";
  756. $jsonArray['result1'] = habit_get_stat_streak($section['Activity'], $moodlog);
  757. $jsonArray['result2'] = habit_get_stat_missed($section['Activity'], $moodlog);
  758. $jsonArray['result3'] = habit_get_stat_top($section['Activity'], $moodlog);
  759.  
  760. }
  761. if($section['Category'][1] == "-"){
  762. $jsonArray['align'] = "bad";
  763. $jsonArray['calCol'] = "#e81b1b";
  764. $jsonArray['title1'] = "Streak";
  765. $jsonArray['title2'] = "Top";
  766. $jsonArray['title3'] = "Total (Year)";
  767. $jsonArray['result1'] = habit_get_stat_missed($section['Activity'], $moodlog);
  768. $jsonArray['result2'] = habit_get_stat_top_missing($section['Activity'], $moodlog);
  769. $jsonArray['result3'] = habit_get_stat_year($section['Activity'], $moodlog);
  770. }
  771. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  772. if ($todayCheck) {
  773. $jsonArray['buttonClass'] = 'button-'.$jsonArray['align'];
  774. }
  775. break;
  776. }
  777. if($section['Category'][0] == "w" && $section['Activity'] == $word){
  778. $found = true;
  779. $jsonArray['freq'] = "w";
  780.  
  781. $multiplier = $section['Category'][1];
  782. $jsonArray['align'] = "good";
  783. $jsonArray['title1'] = "Streak";
  784. $jsonArray['title2'] = "Missed";
  785. $jsonArray['title3'] = "Top";
  786. $jsonArray['result1'] = habit_get_stat_streak_week($section['Activity'], $multiplier, $moodlog);
  787. $jsonArray['result2'] = habit_get_stat_missed_week($section['Activity'], $multiplier, $moodlog);
  788. $jsonArray['result3'] = habit_get_stat_top_week($section['Activity'], $multiplier, $moodlog);
  789. $todayCheck = habit_check_daily($section['Activity'], $moodlog);
  790. $weekCount = habit_count_weekly($section['Activity'], $moodlog);
  791. $jsonArray['weekSoFar'] = $weekCount;
  792. $jsonArray['weekGoal'] = $multiplier;
  793.  
  794. if( $weekCount < $multiplier )
  795. if($todayCheck)
  796. $jsonArray['buttonText'] = $weekCount;
  797. if( $todayCheck ) {
  798. $jsonArray['buttonClass'] = 'button-neut';
  799. if($weekCount >= $multiplier)
  800. $jsonArray['buttonClass'] = 'button-good';
  801. }
  802. break;
  803. }
  804. }
  805.  
  806. if($found)
  807. return json_encode($jsonArray);
  808. return false;
  809. }
  810.  
  811. /***
  812. * creating calendar stuff
  813. */
  814. // Function to get the ISO week numbers in a month
  815. function getISOWeeksInMonth($year, $month) {
  816. $firstDay = new \DateTime("$year-$month-01");
  817. $lastDay = new \DateTime($firstDay->format('Y-m-t'));
  818.  
  819. // Calculate ISO week numbers for each day
  820. $isoWeeks = [];
  821. $currentDay = clone $firstDay;
  822.  
  823. while ($currentDay <= $lastDay) {
  824. $isoWeek = $currentDay->format('W');
  825. $isoWeeks[] = $isoWeek;
  826. $currentDay->modify('+1 day');
  827. }
  828.  
  829. return array_unique($isoWeeks);
  830. }
  831.  
  832. // Function to check if a date is a Sunday
  833. function isSunday($date) {
  834. return $date->format('w') == 0;
  835. }
  836.  
  837. function create_new_weekly_cal(){
  838. $calHTML = "";
  839. // Get current date
  840. $currentDate = new \DateTime();
  841. // Create an array to store months
  842. $months = [];
  843.  
  844. // Loop through the current month to 23 months ago and store in the array
  845. for ($i = 0; $i < 23; $i++) {
  846. $year = $currentDate->format('Y');
  847. $month = $currentDate->format('m');
  848.  
  849. // Get the ISO week numbers in the month
  850. $isoWeeksInMonth = getISOWeeksInMonth($year, $month);
  851.  
  852. // Store the month data in the array
  853. $months[] = [
  854. 'year' => $year,
  855. 'month' => $month,
  856. 'isoWeeks' => $isoWeeksInMonth,
  857. ];
  858.  
  859. // Move forward one month for the next iteration
  860. $currentDate->sub(new DateInterval('P1M'));
  861. }
  862.  
  863. // Reverse the array to display the oldest month first
  864. $months = array_reverse($months);
  865.  
  866. // Create the grid
  867. $calHTML .= '<div class="grid-container">';
  868.  
  869. // Loop through the months array to output the grid
  870. foreach ($months as $monthData) {
  871. $year = $monthData['year'];
  872. $month = $monthData['month'];
  873. $isoWeeksInMonth = $monthData['isoWeeks'];
  874.  
  875. $calHTML .= '<div class="month">';
  876. //echo "<h3>{$year}-{$month}</h3>";
  877. $calHTML .= '<div class="week-container">';
  878. // Keep the last ISO week if it's December and the last day is Sunday
  879. if ($month === '12' && isSunday(new \DateTime("$year-$month-31"))) {
  880. foreach ($isoWeeksInMonth as $isoWeek) {
  881. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  882. }
  883. } else {
  884. // Exclude the last ISO week when outputting weeks
  885. $keys = array_keys($isoWeeksInMonth);
  886. $lastKey = end($keys);
  887. foreach ($isoWeeksInMonth as $isoWeekKey => $isoWeek) {
  888. if ($isoWeekKey === $lastKey) {
  889. continue;
  890. }
  891. $calHTML .= "<div class='week' id='{$year}-{$month}-{$isoWeek}' title='{$year}-{$month}, Week {$isoWeek}'></div>";
  892. }
  893. }
  894.  
  895. $calHTML .= '</div>';
  896. $calHTML .= '</div>';
  897. }
  898.  
  899. $calHTML .= '</div>';
  900. return $calHTML;
  901. }
  902.  
  903.  
  904. ?>
Buy Me A Coffee